Reference · Records and errors

err

signature
err(any=) -> error(any)

What it does

An error value carrying a payload: text, a number, a map, anything. An error value is a value, not a stop: it can be stored, printed, returned, compared, checked with is_err, read with .error, replaced with ?? or passed on from a function with ?. Builtins such as int("x") and json.parse return error values made this way. Without an argument the payload is null.

Examples

let e = err("no such user")print(e, type_of(e), e.error)fn find(id: int) -> map ! str = if id == 1 { { id: 1, name: "Ann" } } else { err(`no user ${id}`) }print(find(1), find(2))print(find(2) ?? { name: "anon" })
output
err("no such user") error no such user{ id: 1, name: "Ann" } err("no user 2"){ name: "anon" }

Edge cases

let e = err({ code: 404, msg: "gone" })print(e.error.code, e.error.msg)print(err(), err("x") == err("x"), err("x") == err("y"))print(err("x") ?? "fallback", 0 ?? "fallback")print(err(err("inner")).error)
output
404 goneerr(null) true falsefallback 0err("inner")

Errors

err itself never fails. Using an error value as if it were its payload is a runtime error: adding to one, indexing it, calling it. A function whose signature declares ! str and returns an error with another payload is a runtime error too, and quillo check reports it before the program runs.

this one fails
print(err("x") + 1)
output
line 1: cannot add error and int

Chaining

With ? the error of a call ends the function with that error; with ?? it becomes a default. match can take an error apart:

fn parse_age(s: str) -> int ! str = match int(s) { int as n if n >= 0 => n, _ => err(`bad age: ${s}`) }fn next_year(s: str) -> int ! str = parse_age(s)? + 1print(next_year("41"), next_year("x"))print(match int("x") { error as e => `failed: ${e.error}`, _ => "ok" })
output
42 err("bad age: x")failed: not an int: "x"

See also

is_err, unwrap, int, json.parse

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

Thread 0 posts

On 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