Skip to main content

Class: ExpectedError<TCode, TValues>

Defined in: index.ts:84

Base class for expected errors: outcomes an operation anticipates and handles — invalid input, a missing resource, a degraded state the caller already reacted to. They still reach the caller, so the message is shown; they are just not faults, and an error tracker must not open an issue for them. Bugs, infrastructure failures and unexpected third-party errors stay a plain Error.

Extend it to add your own kinds:

class PaymentDeclinedError extends ExpectedError {}

name defaults to the subclass's constructor name. A minifier that renames classes changes it too, so a subclass whose name is matched downstream (an error type a client branches on) pins it with override name = '…'.

Extends​

  • Error

Extended by​

Type Parameters​

Type ParameterDefault type
TCode extends string | undefinedstring | undefined
TValues extends ExpectedErrorValues | undefinedExpectedErrorValues | undefined

Implements​

Constructors​

Constructor​

new ExpectedError<TCode, TValues>(message, options?): ExpectedError<TCode, TValues>

Defined in: index.ts:114

Parameters​

ParameterType
messagestring
optionsExpectedErrorOptions<TCode, TValues>

Returns​

ExpectedError<TCode, TValues>

Overrides​

Error.constructor

Properties​

code​

readonly code: TCode

Defined in: index.ts:103

A subclass narrows it through the type parameter, e.g. extends ExpectedError<'CARD_DECLINED' | 'CARD_EXPIRED'>.

Implementation of​

ExpectedErrorLike.code


expected​

readonly expected: true

Defined in: index.ts:97

The discriminant isExpectedError reads. A marker rather than instanceof because a bundler may inline its own copy of this class, and an error thrown from that copy is not an instance of yours.

Implementation of​

ExpectedErrorLike.expected


messageRef?​

optional messageRef?: MessageRef

Defined in: index.ts:112

Mutable: a boundary may attach it before rendering.

Implementation of​

ExpectedErrorLike.messageRef


values​

readonly values: TValues

Defined in: index.ts:109

A subclass narrows it per code through the type parameter, so the placeholders its copy interpolates are a checked contract.

Implementation of​

ExpectedErrorLike.values