diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fc01ca..a3f2236 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,6 +138,7 @@ set(TEST_SRCS test/TestSystem.cxx test/testMemPool.cxx test/testTimer.cxx + test/testExceptions.cxx ) foreach (test ${TEST_SRCS}) diff --git a/include/Common/Exceptions.h b/include/Common/Exceptions.h index ab2922f..463d091 100644 --- a/include/Common/Exceptions.h +++ b/include/Common/Exceptions.h @@ -29,7 +29,10 @@ struct ObjectNotFoundError : virtual ExceptionBase { const char *what() const noexcept override { - return "Object not found error"; + std::string message = "Object not found: "; + auto* errinfo = boost::get_error_info(*this); + message += errinfo ? *boost::get_error_info(*this) : "(object_name not specified)"; + return strdup(message.c_str()); } }; diff --git a/test/testExceptions.cxx b/test/testExceptions.cxx new file mode 100644 index 0000000..d1533d8 --- /dev/null +++ b/test/testExceptions.cxx @@ -0,0 +1,50 @@ +#include "../include/Common/Exceptions.h" + +#define BOOST_TEST_MODULE Exceptions test +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace AliceO2::Common; +using boost::test_tools::output_test_stream; + +void foo() +{ + BOOST_THROW_EXCEPTION(ObjectNotFoundError() << errinfo_object_name("object1")); +} + +void bar() +{ + BOOST_THROW_EXCEPTION(ObjectNotFoundError()); +} + +BOOST_AUTO_TEST_CASE(exceptions_test) +{ + BOOST_CHECK_THROW(foo(), ObjectNotFoundError); + + try { + foo(); + } catch (ObjectNotFoundError& e) { + cout << e.what() << endl; + output_test_stream output; + output << e.what(); + BOOST_CHECK( !output.is_empty( false ) ); + BOOST_CHECK( output.is_equal( "Object not found: object1" ) ); + } + + try { + bar(); + } catch (ObjectNotFoundError& e) { + cout << e.what() << endl; + output_test_stream output; + output << e.what(); + BOOST_CHECK(!output.is_empty(false)); + BOOST_CHECK(output.is_equal("Object not found: (object_name not specified)")); + } +}