Reference · Records and errors

is_err

signature
is_err(any) -> bool

What 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

print(is_err("err"), is_err(err("x").error), is_err(err(err("x")).error))print(is_err(err("x") ?? 1))
output
false false truefalse

Errors

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
null

Chaining

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] truetrue

See also

err, unwrap, int, filter, any

Thread
8Useful? Like it, or add to its thread below.

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