Skip to content

flare ​

@c9up/flare: the exceptions every ream package raises. An error has a machine-readable code, an HTTP status and, when useful, a help text. Guide: Exception handling.

Installation ​

bash
pnpm add @c9up/flare

Every @c9up package depends on flare, and @c9up/ream re-exports Exception, createError, RuntimeException and InvalidArgumentsException. Flare has no dependency and runs in Node.js, in browsers and in React Native.

Entry points ​

ImportContents
@c9up/flareException, createError, RuntimeException, InvalidArgumentsException, format.

Exports ​

ExportKindDescription
ExceptionclassThe base class of every ream error.
createErrorfunctionDeclares an error class in one line.
RuntimeExceptionclassA failure at run time that no more specific error describes.
InvalidArgumentsExceptionclassA function received arguments it cannot work with.
ErrorClassinterfaceThe type of the class createError returns.
ExceptionOptionsinterfaceErrorOptions (cause) plus code?: string and status?: number.
formatfunctionFills %s, %d, %i, %f, %j, %o, %O and %% placeholders, like Node.js util.format.

Exception ​

A subclass sets its metadata once, as static properties. The constructor copies them to the instance.

ts
import { Exception } from '@c9up/flare'

export class E_ROW_NOT_FOUND extends Exception {
  static status = 404
  static code = 'E_ROW_NOT_FOUND'
  static message = 'Row not found'
}
MemberSignatureDescription
constructornew Exception(message?: string, options?: ExceptionOptions)The message falls back to the static message, the status to the static status, then 500.
codestring | undefinedThe machine-readable code. Absent when the class has none.
statusnumberThe HTTP status the error maps to.
helpstring | undefinedAdvice to fix the error, from the static help.
causeunknownThe error that caused this one, when given.
toStringtoString(): stringName [CODE]: message, or Name: message without a code.

createError ​

ts
createError<T extends unknown[] = never>(message: string, code: string, status?: number): ErrorClass<T>

Returns a new Exception subclass. The message may hold placeholders; their values are passed as an array:

ts
import { createError } from '@c9up/flare'

export const E_RESOURCE_NOT_FOUND = createError<[number]>(
  'Unable to find resource with id %d',
  'E_RESOURCE_NOT_FOUND',
  404,
)

throw new E_RESOURCE_NOT_FOUND([42])
throw new E_RESOURCE_NOT_FOUND([42], { cause: error })

How a package exposes its errors ​

Each package declares its errors in src/errors.ts and exports them as one namespace, errors:

ts
import { errors } from '@c9up/bay'

try {
  await queue.dispatch(job)
} catch (error) {
  if (error instanceof errors.E_QUEUE_NOT_INITIALIZED) {
    // …
  }
}

You can also test error.code, which every error index row documents.

Errors ​

CodeRaised when
E_RUNTIME_EXCEPTIONRuntimeException: a failure at run time that no more specific error describes
E_INVALID_ARGUMENTS_EXCEPTIONInvalidArgumentsException: a function received arguments it cannot work with

See also ​