{"id":25619,"library":"generic-errors","title":"generic-errors","description":"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.","status":"active","version":"2.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/MauriceButler/generic-errors","tags":["javascript","error","errors","http","custom","generic","code","404","200"],"install":[{"cmd":"npm install generic-errors","lang":"bash","label":"npm"},{"cmd":"yarn add generic-errors","lang":"bash","label":"yarn"},{"cmd":"pnpm add generic-errors","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Named destructured import works in CommonJS, but module.exports = { ... } object; both patterns are valid.","wrong":"const { NotFound } = require('generic-errors');","symbol":"NotFound","correct":"const errors = require('generic-errors');\nconst notFound = new errors.NotFound('message');"},{"note":"ESM import is available. The package does not have a default export.","wrong":"const BaseError = require('generic-errors').BaseError;","symbol":"BaseError","correct":"import { BaseError } from 'generic-errors';"},{"note":"No default export; must use named import.","wrong":"import BadRequest from 'generic-errors';","symbol":"BadRequest","correct":"const { BadRequest } = require('generic-errors');"}],"quickstart":{"code":"const errors = require('generic-errors');\n\ntry {\n  throw new errors.NotFound('Resource not found');\n} catch (err) {\n  console.log(err.message);        // 'Resource not found'\n  console.log(err.statusCode);     // 404\n  console.log(err instanceof errors.BaseError); // true\n}\n\n// With additional properties\nconst err2 = new errors.Unprocessable({ message: 'Validation failed', fields: ['email'] });\nconsole.log(err2.fields); // ['email']\n\n// Detection after serialization\nconst serialized = JSON.parse(JSON.stringify(err2));\nconsole.log(errors.BaseError.isGenericError(serialized)); // true","lang":"javascript","description":"Shows importing, throwing, catching, custom properties, and serialized detection."},"warnings":[{"fix":"Update imports: const errors = require('generic-errors'); const notFound = new errors.NotFound();","message":"v2.0.0 changed from exporting a single Error object to an object with named constructors.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always use 'new errors.SomeError()'.","message":"Direct construction without 'new' is deprecated and may break in future.","severity":"deprecated","affected_versions":">=2.1.0"},{"fix":"Use the built-in toJSON or implement your own serialization.","message":"Error properties may be lost if you serialize via JSON.stringify without custom toJSON.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Do not rely on all listed codes being widely recognized.","message":"Some constructors like Teapot (418) are humorous but real HTTP status codes.","severity":"gotcha","affected_versions":">=2.2.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use: const errors = require('generic-errors'); const notFound = new errors.NotFound();","cause":"Using require on this package returns an object, not a class directly.","error":"TypeError: errors.NotFound is not a constructor"},{"fix":"Use named import: import { NotFound, BadRequest } from 'generic-errors';","cause":"No default ESM export.","error":"import errors from 'generic-errors'; // SyntaxError: The requested module 'generic-errors' does not provide an export named 'default'"},{"fix":"Always use 'new errors.NotFound()'.","cause":"Instantiating without 'new' (e.g., errors.NotFound()).","error":"Uncaught Error: toString() failed"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}