Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better string representation of the ObjectNotFoundException (O2-1416) #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ set(TEST_SRCS
test/TestSystem.cxx
test/testMemPool.cxx
test/testTimer.cxx
test/testExceptions.cxx
)

foreach (test ${TEST_SRCS})
Expand Down
5 changes: 4 additions & 1 deletion include/Common/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<errinfo_object_name>(*this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implies that one can only write errinfo_object_name as additional exception info, correct. If this fails, maybe try to extract string?

message += errinfo ? *boost::get_error_info<errinfo_object_name>(*this) : "(object_name not specified)";
Barthelemy marked this conversation as resolved.
Show resolved Hide resolved
return strdup(message.c_str());
}
};

Expand Down
50 changes: 50 additions & 0 deletions test/testExceptions.cxx
Original file line number Diff line number Diff line change
@@ -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 <cmath>
#include <boost/test/unit_test.hpp>
#include <assert.h>
#include <boost/test/output_test_stream.hpp>
#include <iostream>
#include <time.h>

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)"));
}
}