@vsaas/error-handler
v12.0.2
Published
Fork of StrongLoop's strong-error-handler for @vsaas LoopBack projects, providing enhanced error handling and response formatting capabilities
Readme
@vsaas/error-handler
HTTP error writer for Express and LoopBack 3 style applications.
@vsaas/error-handler is a fork of strong-error-handler. The goal of this fork is to stay compatible with the original package for the common LoopBack 3 / Strong Remoting use cases, while making the codebase smaller and easier to maintain.
Compared with the upstream package, this fork intentionally removes a number of legacy concerns:
- TypeScript source with build output in
dist/ - English-only messages
- No i18n catalogs or
strong-globalize - Several legacy files and dependencies removed
- Modern tooling with
tsdown,vitest, andoxlint
The public behavior is still meant to be familiar for applications that already depended on strong-error-handler, but the internals were simplified on purpose to reduce maintenance overhead.
Installation
npm install @vsaas/error-handlerUsage
Express middleware
const express = require('express');
const errorHandler = require('@vsaas/error-handler');
const app = express();
app.use(
errorHandler({
debug: process.env.NODE_ENV !== 'production',
log: true,
}),
);Direct response writer
const errorHandler = require('@vsaas/error-handler');
errorHandler.writeErrorToResponse(new Error('something went wrong'), req, res, {
debug: false,
});Response formats
The handler supports:
application/json/jsontext/html/htmltext/xml/xml
Content type is negotiated from the request Accept header. You can also override it with the _format query parameter.
Options
| Option | Default | Description |
| --------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------ |
| debug | false | Include full error details and stack traces in responses. |
| log | true | Log errors to console.error. |
| sensitiveQueryParamNames | [] | Extra query parameter names to redact from console logs. Merged with the defaults (access_token, password, …). |
| replaceSensitiveQueryParamNames | false | When true, sensitiveQueryParamNames replaces the default redact list instead of adding to it. |
| safeFields | [] | Extra error properties allowed through for safe responses. |
| defaultType | "json" | Fallback response type when negotiation does not resolve one. |
| rootProperty | "error" | Root property for JSON/XML responses. Use false to omit the JSON wrapper. |
| negotiateContentType | true | When false, always use defaultType unless _format overrides it. |
Behavior
4xxresponses keep safe client-facing fields such asmessage,details, andcode.5xxresponses are sanitized by default to avoid leaking internals.- When
debug: true, the full error payload is returned. - Console error logs redact secret query parameters from request URLs. Extra names in
sensitiveQueryParamNamesare additive unlessreplaceSensitiveQueryParamNamesistrue.
Development
npm run build
npm run typecheck
npm run lint
npm testLicense
MIT. See LICENSE.md.
