Skip to content

error

Contains utility functions for error handling.

ClassDescription
CustomStackTraceErrorAn error that wraps an error with a custom stack trace.
ErrorWrapperAn error that wraps a non-Error value that was thrown, preserving the original value in cause.
SilentErrorAn error that is not printed to the console.
InterfaceDescription
CustomStackTraceErrorConstructorParamsParameters for the CustomStackTraceError constructor.
FunctionDescription
emitAsyncErrorEventEmits an asynchronous error event. When a collection window is open (see startCollectingUnhandledAsyncErrors) the error is collected as unhandled unless it is ignored — either because shouldIgnore is true, an startAsyncErrorIgnoreContext scope is active, or a consumer handler is registered.
errorToStringConverts an error to a string representation, including nested causes and the aggregated errors of an AggregateError, with indentation.
getStackTraceGets the current stack trace as a string, excluding the current function call.
isAsyncErrorIgnoreContextActiveChecks whether an startAsyncErrorIgnoreContext scope is currently active.
printErrorPrints an error to the console, including nested causes and optional ANSI sequence clearing.
registerAsyncErrorEventHandlerRegisters an event handler for asynchronous errors.
startAsyncErrorIgnoreContextOpens an ignore context in which async errors are treated as expected rather than unhandled, so the test harness does not fail the test. While the returned disposable is held, an async error emitted directly (see emitAsyncErrorEvent) is not collected as unhandled. Crucially, a fire-and-forget operation scheduled within the scope (e.g. via async!invokeAsyncSafely) is also ignored when it later rejects — even after the scope has exited — because async!addErrorHandler captures the active ignore context at schedule time. A test therefore does not need to drain the operation itself: ts it('does not fail', () => \{ using _ = startAsyncErrorIgnoreContext(); invokeAsyncSafely(() => Promise.reject(new Error('deliberately swallowed'))); \}); Contexts nest; the ignore only ends once every open context has been disposed.
startCollectingUnhandledAsyncErrorsOpens a window in which async errors emitted while no consumer handler is registered are collected as unhandled (see emitAsyncErrorEvent), discarding anything collected by a previous window. Intended for the test harness only: the per-test setup opens a window before each test and drains it after each test via stopCollectingUnhandledAsyncErrors. In production no window is open, so emitting an async error carries no bookkeeping overhead.
stopCollectingUnhandledAsyncErrorsCloses the window opened by startCollectingUnhandledAsyncErrors and returns the unhandled async errors collected while it was open.
throwExpressionThrows an error with the specified message.
VariableDescription
ASYNC_WRAPPER_ERROR_MESSAGEA message of the AsyncWrapperError.