Skip to content

Commit

Permalink
Add error::what tag to store identifier strings
Browse files Browse the repository at this point in the history
This should replace the dynamic messages generated at throw site.

Issue #219
  • Loading branch information
rakhimov committed Oct 15, 2017
1 parent 53ae0cc commit 0443c69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "error.h"

#include <boost/exception/get_error_info.hpp>

namespace scram {

const char Error::kPrefix_[] = "scram error: ";
Expand All @@ -28,6 +30,10 @@ Error::Error(std::string msg)
: msg_(std::move(msg)),
thrown_(kPrefix_ + msg_) {}

const char* Error::what() const noexcept { return thrown_.c_str(); }
const char* Error::what() const noexcept {
if (const char* const* msg = boost::get_error_info<error::what>(*this))
return *msg;
return thrown_.c_str();
}

} // namespace scram
12 changes: 12 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,22 @@
#include <exception>
#include <string>

#include <boost/exception/error_info.hpp>
#include <boost/exception/exception.hpp>

namespace scram {

namespace error {

/// 'what' identifier for errors deriving from std::exception.
///
/// @note This should not be the error message to be printed for users.
/// It is the optional *identifier* string.
/// By default, boost::diagnostic_information_what is used.
using what = boost::error_info<struct tag_what, const char*>;

} // namespace error

/// The Error class is the base class
/// for common exceptions specific to the SCRAM code.
class Error : virtual public std::exception, virtual public boost::exception {
Expand Down

0 comments on commit 0443c69

Please sign in to comment.