Reference · Records and errors

unwrap

signature
unwrap(any) -> any

What it does

The value itself when it is not an error value, and a runtime error when it is. It turns an error value into a stop: the request fails with a 500 and the error's payload in the message. Use it where an error cannot happen in practice and a silent default would hide a bug; use ?? for a default and ? to pass the error on.

Examples

print(unwrap(int("42")), unwrap("a"), unwrap([1, 2]))let config = unwrap(json.parse(`{"port": 8080}`))print(config.port)
output
42 a [1, 2]8080

Edge cases

print(unwrap(null), unwrap(false))print(int("x") ?? 0)
output
null false0

Errors

An error value is a runtime error, not an error value: the message is unwrap of an error: and the payload.

this one fails
print(unwrap(err("missing")))
output
line 1: unwrap of an error: missing
this one fails
print(unwrap(err({ code: 404 })))
output
line 1: unwrap of an error: { code: 404 }

Chaining

print(["1", "2"] | map(int) | map(unwrap))print(`[1, 2]` | json.parse | unwrap | sum)
output
[1, 2]3

See also

err, is_err, int, json.parse

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

Thread 0 posts

On unwrap(): 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