Skip to main content

Interface: ErrorKit<R *extends* Registry>

What createErrorKit returns: the error class plus the construct/throw/narrow helpers built around it.

Defined in: kit.ts:5

What createErrorKit returns: the error class plus the construct/throw/narrow helpers built around it.

Type Parameters

R

R extends Registry

Properties

ErrorClass

readonly ErrorClass: <C>(code: C, ...args: Args<R, C>) => KitError<R, C>

Defined in: kit.ts:89

For instanceof checks or subclassing. See createErrorKit for why it's never shared across kits.

Parameters

code

C

args

...Args<R, C>

Returns

KitError<R, C>

Methods

asError()

asError<C extends string>(error: unknown, code: C, ...args: Args<R, C>): KitError<R>

Defined in: kit.ts:98

An already-typed error as it stands; anything else wrapped under the fallback code with the original on cause.

Type Parameters

C

C extends string

Parameters

error

unknown

code

C

args

...Args<R, C>

Returns

KitError<R>


fail()

fail<C extends string>(code: C, ...args: Args<R, C>): KitError<R, C>

Defined in: kit.ts:94

Constructs and returns (never throws) a typed instance.

Type Parameters

C

C extends string

Parameters

code

C

args

...Args<R, C>

Returns

KitError<R, C>


hasErrorCode()

hasErrorCode<C extends string>(err: unknown, code: C): err is Error & { meta: MetaOf<R[C]> }

Defined in: kit.ts:113

Checked by property, not instanceof, so a duplicated copy of this package still matches; meta is checked too, since code alone could narrow to a meta that isn't actually there.

Type Parameters

C

C extends string

Parameters

err

unknown

code

C

Returns

err is Error & { meta: MetaOf<R[C]> }

Example

try {
  await widgets.get(id)
} catch (err) {
  if (kit.hasErrorCode(err, 'WIDGET_NOT_FOUND')) return res.status(404).json({ widgetId: err.meta.widgetId })
  throw err
}

metaOf()

metaOf<C extends string>(err: KitError<R>, code: C): MetaOf<R[C]>

Defined in: kit.ts:121

Reads err.meta at the shape code declares. Safe once the caller has confirmed err.code === code.

Type Parameters

C

C extends string

Parameters

err

KitError<R>

code

C

Returns

MetaOf<R[C]>

Example

if (err.code === 'WIDGET_NOT_FOUND') kit.metaOf(err, 'WIDGET_NOT_FOUND').widgetId // typed, no cast

rethrowError()

rethrowError<C extends string>(error: unknown, code: C, ...args: Args<R, C>): never

Defined in: kit.ts:100

ErrorKit.asError, thrown rather than returned.

Type Parameters

C

C extends string

Parameters

error

unknown

code

C

args

...Args<R, C>

Returns

never


throwError()

throwError<C extends string>(code: C, ...args: Args<R, C>): never

Defined in: kit.ts:96

ErrorKit.fail, thrown rather than returned.

Type Parameters

C

C extends string

Parameters

code

C

args

...Args<R, C>

Returns

never