Add cpptrace::system_error

This commit is contained in:
Jeremy 2024-05-06 23:01:11 -05:00
parent b1e3179a97
commit 74ed6afc0a
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
3 changed files with 35 additions and 1 deletions

View File

@ -348,6 +348,8 @@ Cpptrace provides an interface for a traced exceptions, `cpptrace::exception`, a
that that generate stack traces when thrown. These exceptions generate relatively lightweight raw traces and resolve
symbols and line numbers lazily if and when requested.
These are provided both as a useful utility and as a reference implementation for traced exceptions.
The basic interface is:
```cpp
namespace cpptrace {
@ -398,7 +400,7 @@ namespace cpptrace {
const char* message() const noexcept override;
};
// All stdexcept errors have analogs here. All have the constructor:
// All stdexcept errors have analogs here. All but system_error have the constructor:
// explicit the_error(
// std::string&& message_arg,
// raw_trace&& trace = detail::get_raw_trace_and_absorb()
@ -413,6 +415,15 @@ namespace cpptrace {
class range_error : public exception_with_message { ... };
class overflow_error : public exception_with_message { ... };
class underflow_error : public exception_with_message { ... };
class system_error : public runtime_error {
public:
explicit system_error(
int error_code,
std::string&& message_arg,
raw_trace&& trace = detail::get_raw_trace_and_absorb()
) noexcept;
const std::error_code& code() const noexcept;
};
}
```

View File

@ -6,6 +6,7 @@
#include <limits>
#include <ostream>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
#include <vector>
@ -437,6 +438,17 @@ namespace cpptrace {
std::exception_ptr nested_ptr() const noexcept;
};
class CPPTRACE_EXPORT system_error : public runtime_error {
std::error_code ec;
public:
explicit system_error(
int error_code,
std::string&& message_arg,
raw_trace&& trace = detail::get_raw_trace_and_absorb()
) noexcept;
const std::error_code& code() const noexcept;
};
// [[noreturn]] must come first due to old clang
[[noreturn]] CPPTRACE_EXPORT void rethrow_and_wrap_if_needed(std::size_t skip = 0);
}

View File

@ -577,6 +577,17 @@ namespace cpptrace {
return user_message.c_str();
}
system_error::system_error(int error_code, std::string&& message_arg, raw_trace&& trace) noexcept
: runtime_error(
message_arg + ": " + std::error_code(error_code, std::generic_category()).message(),
std::move(trace)
),
ec(std::error_code(error_code, std::generic_category())) {}
const std::error_code& system_error::code() const noexcept {
return ec;
}
const char* nested_exception::message() const noexcept {
if(message_value.empty()) {
try {