(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-errorprocedureBase error type with message and optional stack trace.
sigil-error?procedureTest if a value is a sigil-error struct.
sigil-error-messageprocedureGet the message field of a sigil-error struct.
sigil-error-stack-traceprocedureGet the stack-trace field of a sigil-error struct.
%type-error--typevariableGet the stack-trace field of a sigil-error struct.
type-errorprocedureType mismatch error with procedure name and expected/got types.
(type-error message: "car: expected pair"
procedure-name: "car"
expected-type: "pair")type-error?procedureTest if a value is a type-error struct.
type-error-messageprocedureGet the message field of a type-error struct.
type-error-stack-traceprocedureGet the stack-trace field of a type-error struct.
type-error-procedure-nameprocedureGet the procedure-name field of a type-error struct.
type-error-expected-typeprocedureGet the expected-type field of a type-error struct.
type-error-got-typeprocedureGet the got-type field of a type-error struct.
type-error->sigil-errorprocedureGet the got-type field of a type-error struct.
%arity-error--typevariableGet the got-type field of a type-error struct.
arity-errorprocedureWrong argument count error.
(arity-error message: "foo: expected 2 arguments, got 3"
procedure-name: "foo"
expected-count: 2
got-count: 3)arity-error?procedureTest if a value is a arity-error struct.
arity-error-messageprocedureGet the message field of a arity-error struct.
arity-error-stack-traceprocedureGet the stack-trace field of a arity-error struct.
arity-error-procedure-nameprocedureGet the procedure-name field of a arity-error struct.
arity-error-expected-countprocedureGet the expected-count field of a arity-error struct.
arity-error-got-countprocedureGet the got-count field of a arity-error struct.
arity-error->sigil-errorprocedureGet the got-count field of a arity-error struct.
%unbound-error--typevariableGet the got-count field of a arity-error struct.
unbound-errorprocedureUnbound variable error.
(unbound-error message: "unbound variable 'foo'"
variable-name: "foo")unbound-error?procedureTest if a value is a unbound-error struct.
unbound-error-messageprocedureGet the message field of a unbound-error struct.
unbound-error-stack-traceprocedureGet the stack-trace field of a unbound-error struct.
unbound-error-variable-nameprocedureGet the variable-name field of a unbound-error struct.
unbound-error->sigil-errorprocedureGet the variable-name field of a unbound-error struct.
%range-error--typevariableGet the variable-name field of a unbound-error struct.
range-errorprocedureIndex 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?procedureTest if a value is a range-error struct.
range-error-messageprocedureGet the message field of a range-error struct.
range-error-stack-traceprocedureGet the stack-trace field of a range-error struct.
range-error-procedure-nameprocedureGet the procedure-name field of a range-error struct.
range-error-indexprocedureGet the index field of a range-error struct.
range-error-min-boundprocedureGet the min-bound field of a range-error struct.
range-error-max-boundprocedureGet the max-bound field of a range-error struct.
range-error->sigil-errorprocedureGet the max-bound field of a range-error struct.
%io-error--typevariableGet the max-bound field of a range-error struct.
io-errorprocedureI/O error with path and operation.
(io-error message: "file not found"
path: "/tmp/foo.txt"
operation: "open")io-error?procedureTest if a value is a io-error struct.
io-error-messageprocedureGet the message field of a io-error struct.
io-error-stack-traceprocedureGet the stack-trace field of a io-error struct.
io-error-pathprocedureGet the path field of a io-error struct.
io-error-operationprocedureGet the operation field of a io-error struct.
io-error->sigil-errorprocedureGet the operation field of a io-error struct.
make-errorprocedureCreate a generic error exception.
(raise (make-error "Something went wrong"))
(raise (make-error "Invalid input" input-value))error?procedureTest if an exception is a generic error.
vm-error?procedureTest 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))exception->errorprocedureConvert 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-exceptionprocedureFormat 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 failedformat-stack-traceprocedureFormat 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 allprint-exceptionprocedurePrint an exception to standard output.
(guard (exn
(else (print-exception exn)))
(error "failed"))make-exceptionvariable(No description)
make-exception-with-tracevariable(No description)
exception?variable(No description)
exception-kindvariable(No description)
exception-messagevariable(No description)
exception-irritantsvariable(No description)
exception-stack-tracevariable(No description)
raisevariable(No description)
raise-continuablevariable(No description)
errorvariable(No description)
with-exception-handlervariable(No description)
current-exception-handlervariable(No description)
guardvariable(No description)