Back to Gleam

Runtime errors

docs/runtime-errors.md

1.16.07.3 KB
Original Source

Runtime errors

There are several runtime errors that Gleam code can throw. This documentation lists them and their runtime properties.

On Erlang runtime errors are Erlang maps thrown with erlang:error/1, having at least these properties:

KeyTypeValue
gleam_errorAtomSee individual errors
messageStringSee individual errors
fileStringA path to the original source file location
moduleStringThe module the error occurred in
functionStringThe function the error occurred in
lineIntThe line number the error occurred on

On JavaScript runtime errors are instances of the JavaScript Error class, having at least these properties added to them:

KeyTypeValue
gleam_errorStringSee individual errors
messageStringSee individual errors
moduleStringThe module the error occurred in
functionStringThe function the error occurred in
lineNumberThe line number the error occurred on

Todo

A panic that indicates that the code has not yet been completed, intended for use in development.

gleam
todo
todo as "some message"
KeyErlang ValueJavaScript Value
gleam_errortodo"todo"
messageThe given messageThe given message

Panic

An explicit panic to unconditionally error.

gleam
panic
panic as "some message"
KeyErlang ValueJavaScript Value
gleam_errorpanic"panic"
messageThe given messageThe given message

Let assert

An inexhaustive pattern match, erroring if the pattern does not match.

gleam
let assert Ok(x) = something()
let assert Error(e) = something() as "This should fail"
KeyErlang ValueJavaScript Value
gleam_errorlet_assert"let_assert"
messageThe given messageThe given message
valueThe unmatched valueThe unmatched value
startThe byte-index of the start of the let assert statementThe byte-index of the start of the let assert statement
endThe byte-index of the end of the let assert statementThe byte-index of the end of the let assert statement
pattern_startThe byte-index of the start of the asserted patternThe byte-index of the start of the asserted pattern
pattern_endThe byte-index of the end of the asserted patternThe byte-index of the end of the asserted pattern

Assert

An assertion of a boolean value.

The error format of assert differs based on the expression that is asserted. It always has these fields:

KeyErlang ValueJavaScript Value
gleam_errorassert"assert"
messageThe given messageThe given message
kindThe kind of asserted expressionThe kind of asserted expression
startThe byte-index of the start of the assert statementThe byte-index of the start of the assert statement
endThe byte-index of the end of the assert expressionThe byte-index of the end of the assert expression
expression_startThe byte-index of the start of the asserted expressionThe byte-index of the start of the asserted expression

But, depending on the expression that was asserted, it contains additional information which can be used to diagnose the error.

Binary operators

gleam
assert level >= 30
KeyErlang TypeJavaScript TypeValue
kindAtomStringbinary_operator
operatorAtomStringThe binary operator that was used
leftMapObjectThe left hand side of the operator
rightMapObjectThe left hand side of the operator

Function calls

gleam
assert check_some_property(a, b, c)
KeyErlang TypeJavaScript TypeValue
kindAtomStringfunction_call
argumentsList of mapArray of objectsThe arguments of the asserted function

Other expressions

gleam
assert other_expression
KeyErlang TypeJavaScript TypeValue
kindAtomStringexpression
expressionMapObjectThe value of the asserted expression

The expression maps have this structure:

KeyErlang TypeJavaScript TypeValue
valueanyanythe value the expression evaluated to at runtime
kindAtomStringliteral or expression or unevaluated
startIntNumberThe byte-index of the start of this expression in the source code
endIntNumberThe byte-index of the end of this expression in the source code

If the expression is a literal, such as True or 15, it will have the literal kind. This signals that its value is not runtime dependent, and may not need to be printed.

If the expression is on the right hand side of a short-circuiting operator, like || or &&, it might not be evaluated. If the operator short-circuits, the right hand side expression will have kind unevaluated.