API reference

Error guards

Narrowing a HapiError without instanceof.

Every variant has a guard, and isHapiError covers the union. Guards test the tag field rather than the prototype chain, so they work across realms and after serialization.

import {
  isApiHttpError, isApiTransportError, isApiAbortError,
  isApiRequestValidationError, isApiPathValidationError,
  isApiResponseValidationError, isApiConfigurationError,
  isApiUnknownError, isHapiError,
} from '@tesyl/hapi';

Prefer a switch on err.tag when you handle several cases — the compiler checks exhaustiveness for you. Reach for a guard when you care about exactly one.

Constructing errors

The makeApi*Error factories are exported for tests and for custom fetchers that want to raise a tagged failure directly. They take an ErrorContext of { service, endpoint, method, path }.

toHapiError

toHapiError(e: unknown, context: ErrorContext, signal?: AbortSignal) => HapiError

  • The normaliser the pipeline uses at its outer boundary. Returns an existing HapiError unchanged, recognises aborts by name, and tags everything else as unknown with the original on cause.
  • Exported so a custom transport can guarantee the same closed union.