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.

Vyi
import { Error, ErrStack, capture } from "@errors"

Exports

NameKindSummary
Errorstructan error with a message and the call stack where it was built
Error.newfunctionbuilds an Error and records the stack
@errmsg, @errstackmethods on Errorits message and its stack
ErrStackstructa recorded call stack of up to 32 calls
capturefunctionrecords 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

MemberSignatureResult
newfn (Error) new(msg: [*con]u8) Erroran Error holding msg and the stack at this call
@errmsgfn (e: Error) @errmsg() [*con]u8msg
@errstackfn (e: *con Error) @errstack() *con ErrStacka pointer to stack
fieldspub 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 called Error.new; the Error.new call itself is not included.
  • Error is an ordinary struct, used by value. Building one allocates nothing.
  • When an Err(Error) reaches the end of main, the program prints error: and the message, then one at file:line:col line per recorded call, innermost first, on standard error, and exits with status 1.
Vyi
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:

text
error: config not found
  at main.vyi:9:20

ErrStack

FieldTypeMeaning
framespub frames: [32]Frame = [32]Frame[]the recorded calls, oldest first, in frames[0] to frames[len - 1]
lenpub len: i32how 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 len to 31 are zero frames: an empty file, 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-linux and arm64-linux; @trace limits). 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:

Vyi
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.