Reference · Records and errors
is_err
signature
is_err(any) -> boolWhat it does
Whether a value is an error value: true for anything made by err or returned as
an error by a builtin such as int("x"), false for everything else, null
included. It is the check to make before reading .error, which only an error value
has.
Examples
print(is_err(err("x")), is_err(int("x")))print(is_err(1), is_err(null), is_err("err"), is_err(int("42")))let n = int("x")print(if is_err(n) { `failed: ${n.error}` } else { `ok: ${n}` })output
true truefalse false false falsefailed: not an int: "x"Edge cases
- The text
"err"and a string that looks like a message are not errors. - The payload of an error is not an error:
is_err(e.error)isfalseunless the payload is an error itself. err("x") ?? 1is1, so the result of??is never an error.- Without an argument the answer is
false.
print(is_err("err"), is_err(err("x").error), is_err(err(err("x")).error))print(is_err(err("x") ?? 1))output
false false truefalseErrors
is_err never fails. Reading .error on a value that is not an error gives null,
whatever the value is, and error(x) does the same.
let n = int("42")print(n.error)output
nullChaining
As a predicate it goes straight into filter, any and map:
let parsed = ["7", "x", "9"] | map(int)print(parsed | filter(is_err))print(parsed | filter((v) => !is_err(v)), parsed | any(is_err))print(err("x") | is_err)output
[err("not an int: \"x\"")][7, 9] truetrueSee also
Thread
Thread 0 posts
On is_err(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo