CHAN.RUN

ensure

API Reference

@chan.run/ensure — Type-safe errors without the boilerplate.

API Reference

@chan.run/ensure — Type-safe errors without the boilerplate.

Interfaces

InterfaceDescription
FaultErrorMarker interface for errors created by fault.
FaultErrorJSONSerialized representation of a FaultError for JSON transport.
MatchHandlersHandler map for match(). Named keys match fault errors; _ is the fallback.
NamedFaultErrorA fault error whose name is known at the type level.
NamedFaultErrorClassConstructor for a FaultError class with a known name.

Type Aliases

Type AliasDescription
AsyncResultResult of tryAsync — error type narrows when wrapping a declared function.
DeclaredFnA function annotated with declares().
ErrorNamesExtract the name string literals from a tuple of error classes.
FaultErrorClassShorthand for NamedFaultErrorClass with any name.
InferFaultErrorsExtract the error union from a declared function's error classes. Maps [typeof NotFoundError, typeof DbError] → NotFoundError
MergeErrorsMerge error class tuples from multiple declared functions. Use with combines() to combine error surfaces.
SyncResultResult of trySync — error type narrows when wrapping a declared function.
TypedMatchHandlersTyped handler map for match() — requires a handler for every declared error name. Use with errors from declares() for exhaustiveness checking.

Variables

VariableDescription
EnsureErrorBuilt-in error for string-form ensure calls. Match on "EnsureError" to remap to a concrete typed error.

Functions

FunctionDescription
combinesCompose error surfaces from multiple declared functions into one. Use when a function calls several declared functions and can throw any of their errors.
declaresAnnotate a function's error surface — purely type-level, zero runtime cost.
defineErrorDefine a reusable typed error class.
ensureAssert non-null/undefined. Returns the narrowed value or throws.
faultThrow a typed error — from a class or an inline string code.
fromJSONDeserialize a plain object back into a FaultError instance. Requires a registry mapping names to error classes.
matchHandle an error by matching against a handler map keyed by name or code. Fault errors are matched by name, then by code. Native errors (TypeError, AbortError, SyntaxError, etc.) are matched by name. Plain Error (name === "Error") always falls through to _ to avoid catching everything.
toJSONSerialize a FaultError to a plain object for JSON transport (API responses, logs).
tryAsyncRun async code, return a discriminated result — never rejects.
trySyncRun sync code, return a discriminated result — never throws.