4#include "../../catch2/catch.hpp"
17namespace unitTest::error_exception_test
24TEST_CASE(
"exception preserves error context",
"[error::exception]" )
26 const int locationLine = __LINE__ + 1;
30 REQUIRE( locationOnly.
message().empty() );
31 REQUIRE( locationOnly.
line() == locationLine );
32 REQUIRE( locationOnly.
file_name().find(
"exception_test.cpp" ) != std::string::npos );
33 REQUIRE( std::string( locationOnly.
what() ).find(
"exception" ) != std::string::npos );
37 REQUIRE( messageOnly.
message() ==
"custom explanation" );
38 REQUIRE( std::string( messageOnly.
what() ).find(
"custom explanation" ) != std::string::npos );
42 REQUIRE( codeAndMessage.
message() ==
"invalid input" );
43 REQUIRE( std::string( codeAndMessage.
what() ).find(
"invalid input" ) != std::string::npos );
47 REQUIRE( codeOnly.
message().empty() );
48 REQUIRE( std::string( codeOnly.
what() ).find(
"sizeerr" ) != std::string::npos );
55TEST_CASE(
"unwind_exceptions extracts nested messages",
"[error::exception]" )
57 std::vector<std::string> messages;
63 throw std::runtime_error(
"inner failure" );
70 catch(
const std::exception &exception )
75 REQUIRE( messages.size() == 2 );
76 REQUIRE( messages[0].find(
"outer failure" ) != std::string::npos );
77 REQUIRE( messages[1] ==
"inner failure" );
88 std::throw_with_nested(
92 catch(
const std::exception &exception )
97 REQUIRE( messages.size() == 1 );
98 REQUIRE( messages[0].find(
"non-standard nested failure" ) != std::string::npos );
105TEST_CASE(
"print_exceptions formats nested messages",
"[error::exception]" )
107 std::vector<std::string> messages{
"outer failure",
"inner failure" };
108 std::ostringstream captured;
109 std::streambuf *original = std::cerr.rdbuf( captured.rdbuf() );
113 std::cerr.rdbuf( original );
114 REQUIRE( captured.str() ==
"processing failed:\n inner failure\n |-->outer failure\n" );
Augments an exception with the source file and line.
const std::string & message() const
Get the message.
error_t code() const
Get the error code.
const std::string file_name() const
Get the source file.
int line() const
Get the source line.
virtual const char * what() const noexcept
Get the what string.
The mxlib exception class.
@ sizeerr
A size was invalid or calculated incorrectly.
@ exception
An exception was thrown.
@ invalidarg
An argument was invalid.
TEST_CASE("exception preserves error context", "[error::exception]")
Verifies all mx::exception constructors and accessors used by hciReduce error paths.
void print_exceptions(std::vector< std::string > &whats, const std::string &message="exception(s) thrown")
Print nested exceptions to stderr, from the earliest to latest.
void unwind_exceptions(std::vector< std::string > &whats, const std::exception &e)
Extract the explanatory string of nested exceptions, placing them in a vector.