@aglyn/shared-util-errors
Error classes shared across the Aglyn packages: an HTTP response error, an upstream-vendor error, and a factory for namespaced, templated errors with stable codes. A small dependency of other Aglyn packages, usable on its own.
Published to npm from the Aglyn monorepo. Versions follow semantic versioning.
Install
npm install @aglyn/shared-util-errors
No peer dependencies.
What's in it
All exported from the package root:
HttpResponseError(code, message)- anErrorcarrying an HTTP status incode(anHttpStatusCodefrom@aglyn/shared-data-enums).UpstreamServiceError(message, { status, retryable, requestId? })- a failed call to a third-party service.messageis meant to be a fixed sentence that is safe to show a customer; the vendor's own wording is not carried on the object.retryablesays whether the vendor asked to come back later (a rate limit or overload) as opposed to rejecting the request.NsErrorFactory(scope, namespace, templates, nsDelimiter = '/', scopeDelimiter = ':', defaultMessage = 'Error')- buildsNsErrorinstances from a map of flag to message template.create(flag, payload?)fills{$name}placeholders from the payload;childFactory(childNamespace, templates?)derives a factory under a nested namespace.NsError- hascode(namespace/flag),messageandpayload.replaceTemplate(template, payload?, options?)- the{$name}placeholder substitution by itself, andDEFAULT_REPLACER_REGEX.- Types:
ErrorPayload,ErrorTagMessages,ErrorTagPayloads,EventFlag.
Usage
import {
NsErrorFactory,
UpstreamServiceError,
} from '@aglyn/shared-util-errors'
const errors = new NsErrorFactory('my-app', 'files', {
'not-found': "Could not find file '{$file}'",
})
try {
throw errors.create('not-found', { file: 'foo.txt' })
} catch (e) {
// e.code === 'files/not-found'; e.payload.file === 'foo.txt'
}
throw new UpstreamServiceError('The provider is busy. Try again shortly.', {
status: 429,
retryable: true,
})
Import-time effect
This package imports the root of @aglyn/shared-util-tools, and that root defines a set of non-enumerable $_-prefixed methods on Array.prototype when it loads. See that package's README for the exact list.
How it fits
A shared package: generic, with no knowledge of Aglyn's model. Shared packages may only import other shared packages; this one depends on @aglyn/shared-data-enums and @aglyn/shared-util-tools. @aglyn/shared-util-rest-api and @aglyn/plugins-ai depend on it.
License
Apache-2.0. Source: https://github.com/aglyn/aglyn/tree/main/libs/shared/util/errors