Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
error.hh File Reference

This file defines two main structs/classes used in nix error handling. More...

#include "nix/util/suggestions.hh"
#include "nix/util/fmt.hh"
#include "nix/util/fun.hh"
#include "nix/util/config.hh"
#include <concepts>
#include <exception>
#include <list>
#include <memory>
#include <optional>
#include <utility>
#include <algorithm>
#include <type_traits>

Go to the source code of this file.

Classes

struct  nix::LinesOfCode
struct  nix::Trace
struct  nix::ErrorInfo
class  nix::BaseError
class  nix::CloneableError< Derived, Base >
class  nix::Error
class  nix::UsageError
class  nix::UnimplementedError

Macros

#define MakeError(newClass, superClass)
#define nixUnreachableWhenHardened   std::unreachable

Enumerations

enum class  Verbosity : unsigned {
  lvlError = 0 , lvlWarn , lvlNotice , lvlInfo ,
  lvlTalkative , lvlChatty , lvlDebug , lvlVomit
}
enum struct  nix::TracePrint { Default , Always }
enum class  Verbosity

Functions

template<std::integral T>
requires (std::is_unsigned_v<T> && !std::is_same_v<bool, T>)
Verbosity nix::verbosityFromIntClamped (T val)
template<std::integral T>
requires std::is_signed_v<T>
Verbosity nix::verbosityFromIntClamped (T)=delete
void nix::printCodeLines (std::ostream &out, const std::string &prefix, const Pos &errPos, const LinesOfCode &loc)
std::strong_ordering nix::operator<=> (const Trace &lhs, const Trace &rhs)
std::ostream & nix::showErrorInfo (std::ostream &out, const ErrorInfo &einfo, bool showTrace)
void nix::rethrowExceptionCopy (std::exception_ptr ex)
void nix::throwExceptionSelfCheck ()
void nix::panic (std::string_view msg)
int nix::handleExceptions (const std::string &programName, fun< void()> body)
void nix::unreachable (std::source_location loc)

Detailed Description

This file defines two main structs/classes used in nix error handling.

ErrorInfo provides a standard payload of error information, with conversion to string happening in the logger rather than at the call site.

BaseError is the ancestor of nix specific exceptions (and Interrupted), and contains an ErrorInfo.

ErrorInfo structs are sent to the logger as part of an exception, or directly with the logError or logWarning macros. See libutil/tests/logging.cc for usage examples.

Macro Definition Documentation

◆ MakeError

#define MakeError ( newClass,
superClass )
Value:
class newClass : public CloneableError<newClass, superClass> \
{ \
void anchor() override; \
public: \
using CloneableError<newClass, superClass>::CloneableError; \
}

Enumeration Type Documentation

◆ TracePrint

enum struct nix::TracePrint
strong

When a stack frame is printed.

Enumerator
Default 

The default behavior; always printed when --show-trace is set.

Always 

Always printed. Produced by builtins.addErrorContext.

Function Documentation

◆ handleExceptions()

int nix::handleExceptions ( const std::string & programName,
fun< void()> body )

Run a function, printing an error and returning on exception. Useful for wrapping a main function that may throw

Parameters
programNameName of program, usually argv[0]
bodyFunction to run inside the try block
Returns
exit code: 0 if success, 1 if exception does not specify.

◆ operator<=>()

std::strong_ordering nix::operator<=> ( const Trace & lhs,
const Trace & rhs )
inline

An arbitrarily defined value comparison for the purpose of using traces in the key of a sorted container.

◆ panic()

void nix::panic ( std::string_view msg)

Print a message and std::terminate().

◆ rethrowExceptionCopy()

void nix::rethrowExceptionCopy ( std::exception_ptr ex)

Like std::rethrow_exception, but if ex is a BaseError, throw a copy of it rather than the original, so that handlers which modify what they catch (e.g. by adding traces) leave the stored exception alone.

◆ throwExceptionSelfCheck()

void nix::throwExceptionSelfCheck ( )

Throw an exception for the purpose of checking that exception handling works; see 'initLibUtil()'.

◆ unreachable()

void nix::unreachable ( std::source_location loc = std::source_location::current())

Print a basic error message with source position and std::terminate().

Note
: This assumes that the logger is operational