Skip to content

Commit 1ba0edc

Browse files
committed
refactor: replace SystemError with generic StacktraceError for improved error handling
1 parent fcdb741 commit 1ba0edc

9 files changed

Lines changed: 32 additions & 23 deletions

File tree

include/zero/async/promise.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace zero::async::promise {
7777
assert(mCore);
7878

7979
if (mRetrieved)
80-
throw std::logic_error{"Future already retrieved"};
80+
throw error::StacktraceError<std::logic_error>{"Future already retrieved"};
8181

8282
mRetrieved = true;
8383
return Future<T, E>{mCore};
@@ -107,7 +107,9 @@ namespace zero::async::promise {
107107
}
108108

109109
if (state != State::OnlyCallback || !mCore->state.compare_exchange_strong(state, State::Done))
110-
throw std::logic_error{fmt::format("Unexpected promise state: {}", std::to_underlying(state))};
110+
throw error::StacktraceError<std::logic_error>{
111+
fmt::format("Unexpected promise state: {}", std::to_underlying(state))
112+
};
111113

112114
mCore->event.set();
113115
mCore->trigger();
@@ -130,7 +132,9 @@ namespace zero::async::promise {
130132
}
131133

132134
if (state != State::OnlyCallback || !mCore->state.compare_exchange_strong(state, State::Done))
133-
throw std::logic_error{fmt::format("Unexpected promise state: {}", std::to_underlying(state))};
135+
throw error::StacktraceError<std::logic_error>{
136+
fmt::format("Unexpected promise state: {}", std::to_underlying(state))
137+
};
134138

135139
mCore->event.set();
136140
mCore->trigger();
@@ -258,7 +262,9 @@ namespace zero::async::promise {
258262
return;
259263

260264
if (state != State::OnlyResult || !mCore->state.compare_exchange_strong(state, State::Done))
261-
throw std::logic_error{fmt::format("Unexpected promise state: {}", std::to_underlying(state))};
265+
throw error::StacktraceError<std::logic_error>{
266+
fmt::format("Unexpected promise state: {}", std::to_underlying(state))
267+
};
262268

263269
mCore->trigger();
264270
}

include/zero/error.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,14 @@ namespace zero::error {
653653

654654
namespace zero::error {
655655
#if defined(__cpp_lib_stacktrace) && __cpp_lib_stacktrace >= 202011L
656-
class SystemError final : public std::system_error {
656+
template<typename T>
657+
requires std::derived_from<T, std::exception>
658+
class StacktraceError final : public T {
657659
public:
658660
template<typename... Args>
659-
explicit SystemError(Args &&... args)
660-
: std::system_error{std::forward<Args>(args)...},
661-
mMessage{fmt::format("{} {}", std::system_error::what(), std::to_string(std::stacktrace::current(1)))} {
661+
explicit StacktraceError(Args &&... args)
662+
: T{std::forward<Args>(args)...},
663+
mMessage{fmt::format("{} {}", T::what(), std::to_string(std::stacktrace::current(1)))} {
662664
}
663665

664666
[[nodiscard]] const char *what() const noexcept override {
@@ -669,14 +671,15 @@ namespace zero::error {
669671
std::string mMessage;
670672
};
671673
#else
672-
using SystemError = std::system_error;
674+
template<typename T>
675+
using StacktraceError = T;
673676
#endif
674677

675678
template<typename T, typename E>
676679
requires std::is_convertible_v<E, std::error_code>
677680
T guard(std::expected<T, E> &&expected) {
678681
if (!expected)
679-
throw SystemError{expected.error()};
682+
throw StacktraceError<std::system_error>{expected.error()};
680683

681684
if constexpr (std::is_void_v<T>)
682685
return;

src/env.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::optional<std::string> zero::env::get(const std::string &name) {
3232

3333
if (result == 0) {
3434
if (const auto error = GetLastError(); error != ERROR_ENVVAR_NOT_FOUND)
35-
throw error::SystemError{static_cast<int>(error), std::system_category()};
35+
throw error::StacktraceError<std::system_error>{static_cast<int>(error), std::system_category()};
3636

3737
return std::nullopt;
3838
}
@@ -85,7 +85,7 @@ std::map<std::string, std::string> zero::env::list() {
8585
};
8686

8787
if (!ptr)
88-
throw error::SystemError{static_cast<int>(GetLastError()), std::system_category()};
88+
throw error::StacktraceError<std::system_error>{static_cast<int>(GetLastError()), std::system_category()};
8989

9090
std::map<std::string, std::string> envs;
9191

src/filesystem/fs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::filesystem::path zero::filesystem::applicationPath() {
3939

4040
if (const auto length = GetModuleFileNameW(nullptr, buffer.data(), buffer.size());
4141
length == 0 || length == buffer.size())
42-
throw error::SystemError{static_cast<int>(GetLastError()), std::system_category()};
42+
throw error::StacktraceError<std::system_error>{static_cast<int>(GetLastError()), std::system_category()};
4343

4444
return buffer.data();
4545
#elif defined(__linux__)

src/os/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ std::string zero::os::net::stringify(const std::span<const std::byte, 4> ip) {
3737

3838
if (!inet_ntop(AF_INET, ip.data(), address.data(), address.size())) {
3939
#ifdef _WIN32
40-
throw error::SystemError{WSAGetLastError(), std::system_category()};
40+
throw error::StacktraceError<std::system_error>{WSAGetLastError(), std::system_category()};
4141
#else
42-
throw error::SystemError{errno, std::system_category()};
42+
throw error::StacktraceError<std::system_error>{errno, std::system_category()};
4343
#endif
4444
}
4545

@@ -51,9 +51,9 @@ std::string zero::os::net::stringify(const std::span<const std::byte, 16> ip) {
5151

5252
if (!inet_ntop(AF_INET6, ip.data(), address.data(), address.size())) {
5353
#ifdef _WIN32
54-
throw error::SystemError{WSAGetLastError(), std::system_category()};
54+
throw error::StacktraceError<std::system_error>{WSAGetLastError(), std::system_category()};
5555
#else
56-
throw error::SystemError{errno, std::system_category()};
56+
throw error::StacktraceError<std::system_error>{errno, std::system_category()};
5757
#endif
5858
}
5959

src/os/process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ zero::os::process::Command::spawn(const std::array<StdioType, 3> &defaultTypes)
807807
const auto handle = GetStdHandle(typeMapping[i]);
808808

809809
if (handle == INVALID_HANDLE_VALUE)
810-
throw error::SystemError{static_cast<int>(GetLastError()), std::system_category()};
810+
throw error::StacktraceError<std::system_error>{static_cast<int>(GetLastError()), std::system_category()};
811811

812812
if (!handle)
813813
continue;

src/os/windows/process.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ std::expected<std::vector<std::string>, std::error_code> zero::os::windows::proc
190190

191191
Z_DEFER(
192192
if (LocalFree(args))
193-
throw error::SystemError{static_cast<int>(GetLastError()), std::system_category()};
193+
throw error::StacktraceError<std::system_error>{static_cast<int>(GetLastError()), std::system_category()};
194194
);
195195

196196
std::vector<std::string> cmdline;

src/strings/strings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ zero::strings::encode(const std::wstring_view str, const std::string &encoding)
139139

140140
Z_DEFER(
141141
if (iconv_close(cd) != 0)
142-
throw error::SystemError{errno, std::generic_category()};
142+
throw error::StacktraceError<std::system_error>{errno, std::generic_category()};
143143
);
144144

145145
std::string output;
@@ -171,7 +171,7 @@ zero::strings::decode(const std::string_view str, const std::string &encoding) {
171171

172172
Z_DEFER(
173173
if (iconv_close(cd) != 0)
174-
throw error::SystemError{errno, std::generic_category()};
174+
throw error::StacktraceError<std::system_error>{errno, std::generic_category()};
175175
);
176176

177177
std::wstring output;

src/utility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ std::tm zero::localTime(const std::time_t time) {
66

77
#ifdef _WIN32
88
if (const auto result = localtime_s(&tm, &time); result != 0)
9-
throw error::SystemError{result, std::generic_category()};
9+
throw error::StacktraceError<std::system_error>{result, std::generic_category()};
1010
#else
1111
if (!localtime_r(&time, &tm))
12-
throw error::SystemError{errno, std::generic_category()};
12+
throw error::StacktraceError<std::system_error>{errno, std::generic_category()};
1313
#endif
1414

1515
return tm;

0 commit comments

Comments
 (0)