sigildocs

(sigil error)

(sigil error) - Exception Handling

Handle errors gracefully with exceptions. Use guard for structured error handling, create typed exceptions for different error conditions, and format exceptions for user-friendly error messages.

Provides both traditional exception vectors (for raise/guard) and a struct-based error hierarchy for structured access to error details. Use exception->error to convert caught exceptions to rich error structs.

(import (sigil error))

;; Handle errors with guard
(guard (exn
         ((type-error? exn) "wrong type!")
         (else (format "error: ~a" (exception-message exn))))
  (risky-operation))

;; Convert to struct for structured access
(guard (exn
         (else
          (let ((err (exception->error exn)))
            (when (type-error? err)
              (display (type-error-expected-type err))))))
  (car 42))

Exports

sigil-errorprocedure

Base error type with message and optional stack trace.

sigil-error?procedure

Test if a value is a sigil-error struct.

Get the message field of a sigil-error struct.

Get the stack-trace field of a sigil-error struct.

Get the stack-trace field of a sigil-error struct.

type-errorprocedure

Type mismatch error with procedure name and expected/got types.

(type-error message: "car: expected pair"
            procedure-name: "car"
            expected-type: "pair")
type-error?procedure

Test if a value is a type-error struct.

Get the message field of a type-error struct.

Get the stack-trace field of a type-error struct.

Get the procedure-name field of a type-error struct.

Get the expected-type field of a type-error struct.

Get the got-type field of a type-error struct.

Get the got-type field of a type-error struct.

Get the got-type field of a type-error struct.

arity-errorprocedure

Wrong argument count error.

(arity-error message: "foo: expected 2 arguments, got 3"
             procedure-name: "foo"
             expected-count: 2
             got-count: 3)
arity-error?procedure

Test if a value is a arity-error struct.

Get the message field of a arity-error struct.

Get the stack-trace field of a arity-error struct.

Get the procedure-name field of a arity-error struct.

Get the expected-count field of a arity-error struct.

Get the got-count field of a arity-error struct.

Get the got-count field of a arity-error struct.

Get the got-count field of a arity-error struct.

unbound-errorprocedure

Unbound variable error.

(unbound-error message: "unbound variable 'foo'"
               variable-name: "foo")

Test if a value is a unbound-error struct.

Get the message field of a unbound-error struct.

Get the stack-trace field of a unbound-error struct.

Get the variable-name field of a unbound-error struct.

Get the variable-name field of a unbound-error struct.

Get the variable-name field of a unbound-error struct.

range-errorprocedure

Index or value out of range error.

(range-error message: "vector-ref: index 10 out of bounds"
             procedure-name: "vector-ref"
             index: 10
             max-bound: 5)
range-error?procedure

Test if a value is a range-error struct.

Get the message field of a range-error struct.

Get the stack-trace field of a range-error struct.

Get the procedure-name field of a range-error struct.

Get the index field of a range-error struct.

Get the min-bound field of a range-error struct.

Get the max-bound field of a range-error struct.

Get the max-bound field of a range-error struct.

Get the max-bound field of a range-error struct.

io-errorprocedure

I/O error with path and operation.

(io-error message: "file not found"
          path: "/tmp/foo.txt"
          operation: "open")
io-error?procedure

Test if a value is a io-error struct.

Get the message field of a io-error struct.

Get the stack-trace field of a io-error struct.

io-error-pathprocedure

Get the path field of a io-error struct.

Get the operation field of a io-error struct.

Get the operation field of a io-error struct.

make-errorprocedure

Create a generic error exception.

(raise (make-error "Something went wrong"))
(raise (make-error "Invalid input" input-value))
error?procedure

Test if an exception is a generic error.

vm-error?procedure

Test if an exception is a VM-level error.

VM errors are runtime errors from the Sigil VM that don't map to a more specific error type (type-error, arity-error, etc.).

(guard (exn
         ((vm-error? exn) "VM error caught")
         (else "other error"))
  (dangerous-op))

Convert an exception (or any raised value) to a rich error struct.

Maps exception kinds to the appropriate struct type, extracting structured fields from the irritants list. Already-converted struct errors are returned as-is.

(guard (exn
         (else
          (let ((err (exception->error exn)))
            (when (type-error? err)
              (format "expected ~a" (type-error-expected-type err))))))
  (car 42))

Format an exception as a human-readable string.

Works with both old-style exception vectors and new error structs. Includes the error kind, message, structured fields, and stack trace.

(guard (exn
         (else (display (format-exception exn))))
  (error "something failed"))
; Error (error): something failed

Format a stack trace as a string.

By default, only shows frames with named functions, hiding anonymous lambdas to reduce noise from macro expansion and internal implementation. Pass #t to show all frames including anonymous ones.

(format-stack-trace (exception-stack-trace exn))
; =>   "0: my-function\n     at main.sgl:42:5\n  ..."

(format-stack-trace (exception-stack-trace exn) #t)  ; show all

(No description)

(No description)

exception?variable

(No description)

(No description)

(No description)

(No description)

(No description)

raisevariable

(No description)

(No description)

errorvariable

(No description)

(No description)

(No description)

guardvariable

(No description)