aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/error-demo/error-demo.cc16
-rw-r--r--src/libutil/error.hh12
-rwxr-xr-xsrc/nix-build/nix-build.cc2
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<string> what_;
- const string& calcWhat()
+ mutable std::optional<string> 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";