Error handling: Difference between revisions

imported>Pogobanane
add dedicated debugging section
imported>Pogobanane
No edit summary
Line 11: Line 11:
The nix language has some constructs to help with printing messages.
The nix language has some constructs to help with printing messages.


- assert: throw an error [2]
* '''assert''': throw an error [2]


The nix language already comes with some usefult builtin functions [3]:
The nix language already comes with some usefult builtin functions [3]:


- throw: throw an error with a message
* '''throw''': throw an error with a message
- abort: same as throw, but always stop evaluation
* '''abort''': same as throw, but always stop evaluation
- trace: print to stderr
* '''trace''': print to stderr
- traceVerbose: print, but only when in --trace-verbose mode
* '''traceVerbose''': print, but only when in --trace-verbose mode
- break: breakpoint when in --debugger mode
* '''break''': breakpoint when in --debugger mode
- tryEval: catch throws and asserts
* '''tryEval''': catch throws and asserts


Commonly, assert is combined with throw to generate meaningful error messages: assert condition || throw "message"; This pattern is essentially how lib.assertMsg works (see Sec. nixpkgs). [1]
Commonly, assert is combined with throw to generate meaningful error messages: assert condition || throw "message"; This pattern is essentially how lib.assertMsg works (see Sec. nixpkgs). [1]
Line 28: Line 28:
There are three main facilities for printing errors and do print debugging in nixpkgs:
There are three main facilities for printing errors and do print debugging in nixpkgs:


- lib.trivial.* [6]
* lib.trivial.* [6]
     - lib.throwIf and throwIfNot
     * lib.throwIf and throwIfNot
     - lib.warn, warnIf and warnIfNot
     * lib.warn, warnIf and warnIfNot
- lib.debug.*: tracing functions with some pretty printing [7, 9]
* lib.debug.*: tracing functions with some pretty printing [7, 9]
- lib.asserts.*: assert functions [8]
* lib.asserts.*: assert functions [8]


These facilities also expose their attributes directly via lib.* (e.g. lib.throwIf).  
These facilities also expose their attributes directly via lib.* (e.g. lib.throwIf).  
Line 42: Line 42:
The NixOS module system again wraps these library functions and makes them available via module options: [11, 10]
The NixOS module system again wraps these library functions and makes them available via module options: [11, 10]


- config.warnings = [];
* config.warnings = [];
- config.assertions = [];
* config.assertions = [];


An example for a debugging facility in NixOS is running NixOS tests interactively (see [13]).
An example for a debugging facility in NixOS is running NixOS tests interactively (see [13]).