feat(error): add DWARFS_PANIC

This commit is contained in:
Marcus Holland-Moritz 2025-03-15 23:44:13 +01:00
parent 6361d8d814
commit e8e2323eea
2 changed files with 18 additions and 3 deletions

View File

@ -74,16 +74,22 @@ class system_error : public std::system_error {
#define DWARFS_CHECK(expr, message) \
do { \
if (!(expr)) { \
assertion_failed(#expr, message, std::source_location::current()); \
::dwarfs::assertion_failed(#expr, message, \
std::source_location::current()); \
} \
} while (0)
} while (false)
#define DWARFS_PANIC(message) \
do { \
::dwarfs::handle_panic(message, std::source_location::current()); \
} while (false)
#define DWARFS_NOTHROW(expr) \
[&]() -> decltype(expr) { \
try { \
return expr; \
} catch (...) { \
handle_nothrow(#expr, std::source_location::current()); \
::dwarfs::handle_nothrow(#expr, std::source_location::current()); \
} \
}()
@ -96,4 +102,7 @@ handle_nothrow(std::string_view expr, std::source_location loc);
assertion_failed(std::string_view expr, std::string_view message,
std::source_location loc);
[[noreturn]] void
handle_panic(std::string_view message, std::source_location loc);
} // namespace dwarfs

View File

@ -93,4 +93,10 @@ void assertion_failed(std::string_view expr, std::string_view msg,
do_terminate();
}
void handle_panic(std::string_view msg, std::source_location loc) {
std::cerr << "Panic: " << msg << " in " << loc.file_name() << "("
<< loc.line() << ")\n";
do_terminate();
}
} // namespace dwarfs