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
drainCollectedUnhandledAsyncErrorsEmpties the collection window opened by startCollectingUnhandledAsyncErrors and returns what it held, leaving the window open so errors emitted afterwards keep being collected. This is what setup!setup’s beforeEach / afterEach use, rather than stopCollectingUnhandledAsyncErrors: an async error emitted in the gap between two tests (e.g. by a setTimeout the test left pending) would otherwise land on a closed window and vanish unreported.
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 or an startAsyncErrorIgnoreContext scope is active. A registered consumer handler deliberately does not exempt the error: in production it shows the user a Notice, which is not a test asserting that the error was expected. startAsyncErrorIgnoreContext is the single, explicit opt-out.
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. Registering a handler does not mark the errors it receives as expected — see emitAsyncErrorEvent. A test that deliberately triggers an async error opens a startAsyncErrorIgnoreContext scope regardless of who is listening.
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 outside an ignore context 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 empties it after each test via drainCollectedUnhandledAsyncErrors, closing it for good with stopCollectingUnhandledAsyncErrors once the file’s last test has run. 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. Use drainCollectedUnhandledAsyncErrors instead wherever collection must continue afterwards — once the window is closed, a later async error is not collected by anyone.
throwExpressionThrows an error with the specified message.
VariableDescription
ASYNC_WRAPPER_ERROR_MESSAGEA message of the AsyncWrapperError.