'what' string

This commit is contained in:
Ben Burdette 2020-04-23 15:55:34 -06:00
parent 3bc9155dfc
commit 833501f6f1

View file

@ -88,6 +88,13 @@ class BaseError : public std::exception
protected: protected:
string prefix_; // used for location traces etc. string prefix_; // used for location traces etc.
ErrorInfo err; ErrorInfo err;
string what_;
void initWhat()
{
std::ostringstream oss;
oss << err;
what_ = oss.str();
}
public: public:
unsigned int status = 1; // exit status unsigned int status = 1; // exit status
@ -97,30 +104,27 @@ public:
.hint = hintfmt(args...) .hint = hintfmt(args...)
} }
, status(status) , status(status)
{ { initWhat(); }
}
template<typename... Args> template<typename... Args>
BaseError(const Args & ... args) BaseError(const Args & ... args)
: err { .level = lvlError, : err { .level = lvlError,
.hint = hintfmt(args...) .hint = hintfmt(args...)
} }
{ { initWhat(); }
}
BaseError(ErrorInfo e) BaseError(ErrorInfo e)
: err(e) : err(e)
{ { initWhat(); }
}
#ifdef EXCEPTION_NEEDS_THROW_SPEC #ifdef EXCEPTION_NEEDS_THROW_SPEC
~BaseError() throw () { }; ~BaseError() throw () { };
const char * what() const throw () { return err.description.c_str(); } const char * what() const throw () { return what_.c_str(); }
#else #else
const char * what() const noexcept { return err.description.c_str(); } const char * what() const noexcept { return what_.c_str(); }
#endif #endif
const string & msg() const { return err.description; } const string & msg() const { return what_; }
const string & prefix() const { return prefix_; } const string & prefix() const { return prefix_; }
BaseError & addPrefix(const FormatOrString & fs); BaseError & addPrefix(const FormatOrString & fs);