mirror of
https://github.com/NixOS/nix
synced 2025-06-25 06:31:14 +02:00
libmain: Catch logger exceptions in handleExceptions
Avoid std::terminate in case logging code also throws.
This commit is contained in:
parent
b4bea57667
commit
90d1ff4805
1 changed files with 27 additions and 22 deletions
|
@ -317,29 +317,34 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
|
||||||
std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
|
std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
fun();
|
try {
|
||||||
} catch (...) {
|
fun();
|
||||||
/* Subtle: we have to make sure that any `interrupted'
|
} catch (...) {
|
||||||
condition is discharged before we reach printMsg()
|
/* Subtle: we have to make sure that any `interrupted'
|
||||||
below, since otherwise it will throw an (uncaught)
|
condition is discharged before we reach printMsg()
|
||||||
exception. */
|
below, since otherwise it will throw an (uncaught)
|
||||||
setInterruptThrown();
|
exception. */
|
||||||
throw;
|
setInterruptThrown();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
} catch (Exit & e) {
|
||||||
|
return e.status;
|
||||||
|
} catch (UsageError & e) {
|
||||||
|
logError(e.info());
|
||||||
|
printError("Try '%1% --help' for more information.", programName);
|
||||||
|
return 1;
|
||||||
|
} catch (BaseError & e) {
|
||||||
|
logError(e.info());
|
||||||
|
return e.info().status;
|
||||||
|
} catch (std::bad_alloc & e) {
|
||||||
|
printError(error + "out of memory");
|
||||||
|
return 1;
|
||||||
|
} catch (std::exception & e) {
|
||||||
|
printError(error + e.what());
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
} catch (Exit & e) {
|
} catch (...) {
|
||||||
return e.status;
|
/* In case logger also throws just give up. */
|
||||||
} catch (UsageError & e) {
|
|
||||||
logError(e.info());
|
|
||||||
printError("Try '%1% --help' for more information.", programName);
|
|
||||||
return 1;
|
|
||||||
} catch (BaseError & e) {
|
|
||||||
logError(e.info());
|
|
||||||
return e.info().status;
|
|
||||||
} catch (std::bad_alloc & e) {
|
|
||||||
printError(error + "out of memory");
|
|
||||||
return 1;
|
|
||||||
} catch (std::exception & e) {
|
|
||||||
printError(error + e.what());
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue