Registry / http-networking / generic-errors

generic-errors

JSON →
library2.3.1jsnpmunverified

Provides error constructors with common HTTP status codes as numeric properties, supporting both Node.js and browser usage. Current stable version is 2.3.1 (last updated with dependency security patches). Release cadence is ad-hoc with occasional minor updates. Key differentiators: includes a stack-trace ponyfill for Safari compatibility, does not pollute stack depth, and offers detection via BaseError.isGenericError even after JSON serialization. Over 40 error types from 4xx and 5xx families are available.

npm install generic-errors
INSTALL
IMPORT
SIG · GENERIC-ERRORS
G
generic-errors
http-networkingjavascriptv2.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

NotFound
✓ const errors = require('generic-errors'); const notFound = new errors.NotFound('message');
✗ const { NotFound } = require('generic-errors');
Named destructured import works in CommonJS, but module.exports = { ... } object; both patterns are valid.
BaseError
✓ import { BaseError } from 'generic-errors';
✗ const BaseError = require('generic-errors').BaseError;
ESM import is available. The package does not have a default export.
BadRequest
✓ const { BadRequest } = require('generic-errors');
✗ import BadRequest from 'generic-errors';
No default export; must use named import.

Shows importing, throwing, catching, custom properties, and serialized detection.

const errors = require('generic-errors'); try { throw new errors.NotFound('Resource not found'); } catch (err) { console.log(err.message); // 'Resource not found' console.log(err.statusCode); // 404 console.log(err instanceof errors.BaseError); // true } // With additional properties const err2 = new errors.Unprocessable({ message: 'Validation failed', fields: ['email'] }); console.log(err2.fields); // ['email'] // Detection after serialization const serialized = JSON.parse(JSON.stringify(err2)); console.log(errors.BaseError.isGenericError(serialized)); // true
Debug
Known issues
breakingv2.0.0 changed from exporting a single Error object to an object with named constructors.
fix
Update imports: const errors = require('generic-errors'); const notFound = new errors.NotFound();
affects: >=2.0.0
deprecatedDirect construction without 'new' is deprecated and may break in future.
fix
Always use 'new errors.SomeError()'.
affects: >=2.1.0
gotchaError properties may be lost if you serialize via JSON.stringify without custom toJSON.
fix
Use the built-in toJSON or implement your own serialization.
affects: >=0.0.0
gotchaSome constructors like Teapot (418) are humorous but real HTTP status codes.
fix
Do not rely on all listed codes being widely recognized.
affects: >=2.2.0
Errors
Common errors & fixes
TypeError: errors.NotFound is not a constructor
Using require on this package returns an object, not a class directly.
fix
Use: const errors = require('generic-errors'); const notFound = new errors.NotFound();
import errors from 'generic-errors'; // SyntaxError: The requested module 'generic-errors' does not provide an export named 'default'
No default ESM export.
fix
Use named import: import { NotFound, BadRequest } from 'generic-errors';
Uncaught Error: toString() failed
Instantiating without 'new' (e.g., errors.NotFound()).
fix
Always use 'new errors.NotFound()'.
Upgrade
Version history
2.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources
generic-errors — npm install generic-errors · libregistry