Throw instead of exiting from the tile clipping and overzooming code

The code reachable from overzooming a tile no longer calls exit(). It
reports errors by throwing tippecanoe_error, and owns its FILE * and
json_pull * handles through RAII wrappers so that nothing leaks when an
error unwinds the stack.

New files:

* errors.hpp/errors.cpp: the tippecanoe_error exception class, plus
  throw_tippecanoe_error() and throw_perror() helpers.
* raii.hpp: unique_file and unique_json_pull, both std::unique_ptr
  aliases, for FILE * and json_pull *.

exit() becomes throw in clip.cpp, mvt.cpp, mvt.hpp, read_json.cpp,
evaluator.cpp, text.cpp, geometry.cpp, attribute.cpp, and
write_json.hpp. Each tool's main() is now a try/catch around an
inner_main(), so the command-line tools exit with the status they
always did.

Along the way this fixes a z_stream leaked when compress() failed, an
exit() that could run from json_writer's destructor during unwinding,
and an exit() in mvt_value's hash function.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DuCSVGssWNUCpVEE8Yp5HT
This commit is contained in:
Claude
2026-08-07 18:46:09 +00:00
parent 734bba7c78
commit 85547150c2
19 changed files with 272 additions and 170 deletions
+31
View File
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "errors.hpp"
[[noreturn]] void throw_tippecanoe_error(int code, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
char *msg = NULL;
if (vasprintf(&msg, fmt, ap) < 0) {
va_end(ap);
throw tippecanoe_error(code, "error (could not format message)");
}
va_end(ap);
std::string s(msg);
free(msg);
// Remove trailing newline if present (stderr messages often have one)
while (s.size() > 0 && s.back() == '\n') {
s.pop_back();
}
throw tippecanoe_error(code, s);
}
[[noreturn]] void throw_perror(int code, const char *msg) {
throw tippecanoe_error(code, std::string(msg) + ": " + strerror(errno));
}