Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/libmain/include/nix/main/shared.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace nix {

int handleExceptions(const std::string & programName, std::function<void()> fun);

/**
* Don't forget to call initPlugins() after settings are initialized!
* @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests.
Expand Down
29 changes: 0 additions & 29 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,35 +312,6 @@ void printVersion(const std::string & programName)
throw Exit();
}

int handleExceptions(const std::string & programName, std::function<void()> fun)
{
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this

ErrorInfo::programName = baseNameOf(programName);

std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
try {
fun();
} 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;
}

return 0;
}

RunPager::RunPager()
{
if (!isatty(STDOUT_FILENO))
Expand Down
30 changes: 30 additions & 0 deletions src/libutil/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "nix/util/error.hh"
#include "nix/util/environment-variables.hh"
#include "nix/util/exit.hh"
#include "nix/util/signals.hh"
#include "nix/util/terminal.hh"
#include "nix/util/position.hh"
Expand Down Expand Up @@ -455,4 +456,33 @@ void unreachable(std::source_location loc)
panic(std::string_view(buf, std::min(static_cast<int>(sizeof(buf)), n)));
}

int handleExceptions(const std::string & programName, std::function<void()> fun)
{
ReceiveInterrupts receiveInterrupts; // FIXME: need better place for this

ErrorInfo::programName = baseNameOf(programName);

std::string error = ANSI_RED "error:" ANSI_NORMAL " ";
try {
fun();
} 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;
}

return 0;
}

} // namespace nix
10 changes: 10 additions & 0 deletions src/libutil/include/nix/util/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ void throwExceptionSelfCheck();
[[noreturn]]
void panic(std::string_view msg);

/**
* Run a function, printing an error and returning on exception.
* Useful for wrapping a `main` function that may throw
*
* @param programName Name of program, usually argv[0]
* @param fun Function to run inside the try block
* @return exit code: 0 if success, 1 if exception does not specify.
*/
int handleExceptions(const std::string & programName, std::function<void()> fun);

/**
* Print a basic error message with source position and std::terminate().
*
Expand Down
Loading