From 1b801cec407454b904466153dfc56cec6b433f4b Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Thu, 7 May 2020 16:43:36 -0600 Subject: [PATCH] pretending to be const --- src/error-demo/error-demo.cc | 16 ++++++++++++++++ src/libutil/error.hh | 12 ++++++------ src/nix-build/nix-build.cc | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc index 0d3dcf616..89ba5a78d 100644 --- a/src/error-demo/error-demo.cc +++ b/src/error-demo/error-demo.cc @@ -43,6 +43,22 @@ int main() logError(e.info()); } + // current exception + try { + throw DemoError("DemoError handled as a %1%", "std::exception"); + } + catch (...) { + const std::exception_ptr &eptr = std::current_exception(); + try + { + std::rethrow_exception(eptr); + } + catch (std::exception& e) + { + std::cerr << e.what() << std::endl; + } + } + // For completeness sake, info through vomit levels. // But this is maybe a heavy format for those. logger->logEI( diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 2155ad344..19a806cc1 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -82,10 +82,10 @@ class BaseError : public std::exception { protected: string prefix_; // used for location traces etc. - ErrorInfo err; + mutable ErrorInfo err; - std::optional what_; - const string& calcWhat() + mutable std::optional what_; + const string& calcWhat() const { if (what_.has_value()) return *what_; @@ -131,12 +131,12 @@ public: #ifdef EXCEPTION_NEEDS_THROW_SPEC ~BaseError() throw () { }; - const char * what() throw () { return calcWhat().c_str(); } + const char * what() const throw () { return calcWhat().c_str(); } #else - const char * what() noexcept { return calcWhat().c_str(); } + const char * what() const noexcept override { return calcWhat().c_str(); } #endif - const string & msg() { return calcWhat(); } + const string & msg() const { return calcWhat(); } const string & prefix() const { return prefix_; } BaseError & addPrefix(const FormatOrString & fs); diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index 401c8d340..3c2f4f00c 100755 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -368,7 +368,7 @@ static void _main(int argc, char * * argv) shell = drv->queryOutPath() + "/bin/bash"; } catch (Error & e) { - // TODO: append error msg + // TODO: append error msg; warn()? logError(e.info()); printError("warning: %s; will use bash from your environment", e.what()); shell = "bash";