Skip to content

Commit 99d80fe

Browse files
committed
added traced_error
1 parent 184efed commit 99d80fe

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@
5555
[submodule "include/breakpoint/include/ut/breakpoint"]
5656
path = include/breakpoint/include/ut/breakpoint
5757
url = https://gist.github.com/dk949/e36f1a7ecc9a8aff296ee6e76a06ba68
58+
[submodule "include/traced_error/include/ut/traced_error"]
59+
path = include/traced_error/include/ut/traced_error
60+
url = https://gist.github.com/dk949/392c55c65e3c4efb89c7b89bd4390ba4

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ target_link_libraries(MY_TARGET UT::target_name)
4646
* `UT::err`: convenient error type for use with std::expected
4747
* `UT::add_noexcept`: add the `noexcept` specifier to a function type
4848
* `UT::change_observer`: wrapper class that invokes callbacks when it's held value changes
49+
* `UT::traced_error`: an exception type that automatically stores a C++23 standard stacktrace
4950
* `UT::sv_to_num`: convert `std::string_view` to a number
5051
* `UT::copy_traits`: copy cvref qualifiers from one type to another
5152
* `UT::realloc_unique_ptr`: `realloc` functionality for `std::uniqur_ptr`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(libut LANGUAGES CXX)
3+
4+
add_library(libut_target_traced_error INTERFACE)
5+
target_sources(libut_target_traced_error PUBLIC
6+
FILE_SET HEADERS
7+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
8+
FILES
9+
${CMAKE_CURRENT_SOURCE_DIR}/include/ut/traced_error/traced_error.hpp
10+
)
11+
12+
message(STATUS "libut: adding UT::traced_error")
13+
add_library(UT::traced_error ALIAS libut_target_traced_error)
14+
Submodule traced_error added at 47d0165

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(cxx_23_files
3232
${cxx_20_files}
3333
${CMAKE_CURRENT_SOURCE_DIR}/err.cpp
3434
${CMAKE_CURRENT_SOURCE_DIR}/switchboard.cpp
35+
${CMAKE_CURRENT_SOURCE_DIR}/traced_error.cpp
3536
)
3637

3738
function(setup_target target)
@@ -59,3 +60,6 @@ target_compile_features(libut_tests_23 PUBLIC cxx_std_23)
5960
setup_target(libut_tests_17)
6061
setup_target(libut_tests_20)
6162
setup_target(libut_tests_23)
63+
if(NOT MSVC)
64+
target_link_libraries(libut_tests_23 PUBLIC stdc++exp)
65+
endif()

tests/traced_error.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <catch.hpp>
2+
#include <catch2/catch_test_macros.hpp>
3+
#include <ut/traced_error/traced_error.hpp>
4+
using namespace std::string_view_literals;
5+
6+
void throwsTracedError() {
7+
throw ut::TracedError("Hello {}", "world");
8+
};
9+
10+
TEST_CASE("Basic usage", "[traced_error]") {
11+
12+
try {
13+
throwsTracedError();
14+
} catch (ut::TracedError const &t) {
15+
auto trace = t.trace();
16+
INFO(trace);
17+
REQUIRE(!trace.empty());
18+
REQUIRE(t.what() == "Hello world"sv);
19+
}
20+
}
21+
22+
class MyError : public ut::TracedError {
23+
public:
24+
template<typename... Args>
25+
MyError(std::format_string<Args...> fmt, Args &&...args)
26+
: TracedError(1, fmt, std::forward<Args>(args)...) { }
27+
};
28+
29+
void throwsMyError() {
30+
throw MyError("Hello {}", "world");
31+
};
32+
33+
TEST_CASE("Inheriting from exception", "[traced_error]") {
34+
35+
try {
36+
throwsMyError();
37+
} catch (MyError const &t) {
38+
auto trace = t.trace();
39+
INFO(trace);
40+
REQUIRE(!trace.empty());
41+
REQUIRE(t.what() == "Hello world"sv);
42+
} catch (ut::TracedError const &t) {
43+
FAIL("This handler should never be entered");
44+
}
45+
try {
46+
throwsMyError();
47+
} catch (ut::TracedError const &t) {
48+
auto trace = t.trace();
49+
INFO(trace);
50+
REQUIRE(!trace.empty());
51+
REQUIRE(t.what() == "Hello world"sv);
52+
}
53+
}

traced_error.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)