Errors
An error object, err
, can be returned by a function to indicate failure.
-
$err() -> err
$err(message: any) -> err
$err(format_string: str, arg1: any, arg2: any, ...) -> err
-
Creates a new
err
object. Everyerr
has amessage
string and adetails
map.-
If called with no arguments, returns an error with an empty
message
string. -
If called with a single argument, stringifies that argument and uses the result as the error's
message
string. -
Calling this function with more than one argument is equivalent to calling
$fmt()
on those arguments first and using the result as the error'smessage
string.
-
If called with no arguments, returns an error with an empty
You can use the $is_err(arg)
function to check if a value is an err
.
Alternatively, you can use the error-coalescing operator !!
to supply a default value for an operation that might return an error:
var foo = might_fail() !! "default";
Error Message
Stringifying an error returns its message
:
var err = $err("oh no!"); assert $str(err) == "oh no!";
Error Details
Every error object contains a details
map which you can use to store arbitrary data about the error.
You can index into this details
map directly on the err
object itself, e.g.
var err = $err("disaster strikes!"); err["code"] = 123; assert $str(err) == "disaster strikes!"; assert err["code"] == 123;
The details
map of an err
object returned by a try
expression contains "source"
and "line"
entries specifying the source ID and line number of the panic.
Methods
-
:details() -> map
-
Returns the error's
details
map. -
:message() -> str
-
Returns the error's
message
string.