Skip to content

Commit

Permalink
Add cpptrace::system_error
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed May 7, 2024
1 parent b1e3179 commit 74ed6af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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;
};
}
```
Expand Down
12 changes: 12 additions & 0 deletions include/cpptrace/cpptrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <limits>
#include <ostream>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions src/cpptrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 74ed6af

Please sign in to comment.