Docs · Standard library
@errors
@errors holds Error, an error that carries a message and the call stack
where it was built, and capture, which records the call stack as an
ErrStack. Guide
07, Optionals and errors
teaches them, and the
error model states the
rules for @errmsg and @errstack.
import { Error, ErrStack, capture } from "@errors"Exports
| Name | Kind | Summary |
|---|---|---|
Error | struct | an error with a message and the call stack where it was built |
Error.new | function | builds an Error and records the stack |
@errmsg, @errstack | methods on Error | its message and its stack |
ErrStack | struct | a recorded call stack of up to 32 calls |
capture | function | records the current call stack |
A frame in an ErrStack is a Frame from @trace:
file, line, and col of one call in progress. Import Frame from @trace
to name its type.
Error
| Member | Signature | Result |
|---|---|---|
new | fn (Error) new(msg: [*con]u8) Error | an Error holding msg and the stack at this call |
@errmsg | fn (e: Error) @errmsg() [*con]u8 | msg |
@errstack | fn (e: *con Error) @errstack() *con ErrStack | a pointer to stack |
| fields | pub msg: [*con]u8, pub stack: ErrStack |
- The stack is recorded by
new, not where the error is passed on. Its innermost frame is the call into the function that calledError.new; theError.newcall itself is not included. Erroris an ordinary struct, used by value. Building one allocates nothing.- When an
Err(Error)reaches the end ofmain, the program printserror:and the message, then oneat file:line:colline per recorded call, innermost first, on standard error, and exits with status 1.
import { println } from "@fmt"
import { Error } from "@errors"
fn loadConfig(path: [*con]u8) Error!i32 {
return Err(Error.new("config not found"))
}
fn main() !u8 {
if Err(e) = loadConfig("app.conf") {
con stack = e.@errstack()
println(e.@errmsg()) // prints config not found
println(stack.len) // prints 1
println(stack.frames[0].line) // prints 9
}
return Ok(0)
}Left uncaught, as in con port = try loadConfig("app.conf") on line 9 of
main.vyi, the same error prints:
error: config not found
at main.vyi:9:20ErrStack
| Field | Type | Meaning |
|---|---|---|
frames | pub frames: [32]Frame = [32]Frame[] | the recorded calls, oldest first, in frames[0] to frames[len - 1] |
len | pub len: i32 | how many entries of frames are in use |
- It holds at most 32 calls. When more calls are in progress, it keeps the 32 innermost.
- It is a plain value of fixed size. Recording it allocates nothing, so it works while an allocator is failing.
- Entries from
lento 31 are zero frames: an emptyfile, line 0, column 0. - Calls deeper than the call-location table were never recorded (the table holds 4,096 calls on wasm and 2,730 on
x86-64-linuxandarm64-linux;@tracelimits). Below that depth, the stack ends at the deepest recorded call.
capture
fn capture(skip: i32) ErrStack records the calls in progress where it is
called, oldest first. The call to capture itself is not included, and
skip leaves out that many more of the innermost calls: capture(0) is the
whole stack of the function that called capture, capture(1) stops at the
call into that function's caller, and so on.
Use it to give your own error type a stack. Declare @errstack on the type,
and the end of main prints the stack as it does for Error:
import { println } from "@fmt"
import { capture, ErrStack } from "@errors"
struct ParseError { line: i32, stack: ErrStack }
fn (e: ParseError) @errmsg() [*con]u8 {
return "parse failed"
}
fn (e: *con ParseError) @errstack() *con ErrStack {
return &e.stack
}
fn parse() ParseError!i32 {
return Err(ParseError{ line: 3, stack: capture(0) })
}
fn main() !u8 {
if Err(e) = parse() {
println(e.line) // prints 3
println(e.@errstack().len) // prints 1
}
return Ok(0)
}See also: @trace for reading the stack without an error, guide
07, Optionals and errors, guide
08, defer, recover, and panic, and
the error model.