diff options
author | Profpatsch <mail@profpatsch.de> | 2018-07-26 20:45:55 +0200 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2018-09-06 18:14:27 +0200 |
commit | 0e2aa97f3aa1341693885511fd78394b828c6477 (patch) | |
tree | 0e9e069dc0833babd35a917ce081269c54cd2c17 /lib/trivial.nix | |
parent | fc2c606a7ac8fb48be4181f777d029bd75c0c58a (diff) | |
download | nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar.gz nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar.bz2 nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar.lz nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar.xz nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.tar.zst nixpkgs-0e2aa97f3aa1341693885511fd78394b828c6477.zip |
lib/trivial: add assertMsg
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r-- | lib/trivial.nix | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix index e702b8cdcc9..bba284548d9 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -188,6 +188,26 @@ rec { warn = msg: builtins.trace "WARNING: ${msg}"; info = msg: builtins.trace "INFO: ${msg}"; + /* Print a trace message if pred is false. + Intended to be used to augment asserts with helpful error messages. + + Example: + assertMsg false "nope" + => false + stderr> trace: nope + + assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); "" + stderr> trace: foo is not bar, silly + stderr> assert failed at … + + Type: + assertMsg :: Bool -> String -> Bool + */ + assertMsg = pred: msg: + if pred + then true + else builtins.trace msg false; + ## Function annotations |