Skip to content

Error handling

Joakim edited this page Nov 1, 2021 · 29 revisions

Exploration of error handling, inspired by vlang, using error recovery operator ?!.

-- function possibly returning an error
get-user: (id: #number) -> #user | #error
    db.get(table: 'user', id) ? Error "User { id } not found"
    -- returns an #error if db.get() returns #none

-- ignore error, fallback to a default value
user: get-user(7) ?! default-user

-- propagate error, return early
user: get-user(7) ?!
    return Error 'Loading failed'

-- even better, retain original error
user: get-user(7) ?! as cause
    return Error('Loading failed', [cause])
Clone this wiki locally