{"name":"@fastify/error","version":"4.1.0","keywords":["fastify","error","utility","plugin"],"author":{"name":"Tomas Della Vedova"},"license":"MIT","_id":"@fastify/error@4.1.0","maintainers":[{"name":"starptech","email":"deusdustin@gmail.com"},{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"coopflow","email":"community@coopflow.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"allevo","email":"tomallevi@gmail.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"zekth","email":"vince.legoff@gmail.com"},{"name":"rafaelgss","email":"rafael.nunu@hotmail.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"fox1t","email":"maksim@sinik.it"},{"name":"airhorns","email":"harry@harry.me"},{"name":"kibertoad","email":"iselwin@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"},{"name":"galvez","email":"jonasgalvez@gmail.com"},{"name":"simenb","email":"sbekkhus91@gmail.com"},{"name":"gurgunday","email":"hey@gurgun.day"},{"name":"metcoder95","email":"me@metcoder.dev"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"url":"https://james.sumners.info","name":"James Sumners"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/fastify-error#readme","bugs":{"url":"https://github.com/fastify/fastify-error/issues"},"tsd":{"compilerOptions":{"esModuleInterop":true}},"dist":{"shasum":"a6a3a8d2309bd8d3441512dff9a7c739d0c35fe2","tarball":"http://registry-npm.ywwl.com/@fastify/error/download/@fastify/error-4.1.0.tgz","fileCount":15,"integrity":"sha512-KeFcciOr1eo/YvIXHP65S94jfEEqn1RxTRBT1aJaHxY5FK0/GDXYozsQMMWlZoHgi8i0s+YtrLsgj/JkUUjSkQ==","signatures":[{"sig":"MEYCIQCzu01vXLi4rfOWTSJhVYDVn7YYLZWwNi44diOfRADDlgIhALzva59rh0jYdpj2+co/5byrTjJaUAs2Hj2DU2g1fgFM","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":21651,"size":5266,"noattachment":false,"key":"/@fastify/error/-/@fastify/error-4.1.0.tgz"},"main":"index.js","type":"commonjs","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"a8c274c416da0ecdd367901cbe309600288d4585","scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"c8 --100 node --test","test:typescript":"tsd"},"_npmUser":{"name":"gurgunday","email":"hey@gurgun.day"},"repository":{"url":"git+https://github.com/fastify/fastify-error.git","type":"git"},"_npmVersion":"10.9.2","description":"A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.","directories":{},"_nodeVersion":"22.14.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","tsd":"^0.31.0","eslint":"^9.17.0","benchmark":"^2.1.4","neostandard":"^0.12.0"},"_npmOperationalInternal":{"tmp":"tmp/error_4.1.0_1741804741919_0.22208424351399514","host":"s3://npm-registry-packages-npm-production"},"readme":"# @fastify/error\n\n[![CI](https://github.com/fastify/fastify-error/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-error/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/error.svg?style=flat)](https://www.npmjs.com/package/@fastify/error)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nA small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.\n\n### Install\n```\nnpm i @fastify/error\n```\n\n### Usage\n\nThe module exports a function that you can use for consistent error objects, it takes 4 parameters:\n\n```js\ncreateError(code, message [, statusCode [, Base [, captureStackTrace]]])\n```\n\n- `code` (`string`, required) - The error code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_`\n- `message` (`string`, required) - The error message. You can also use interpolated strings for formatting the message.\n- `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP.\n- `Base` (`ErrorConstructor`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)\n- `captureStackTrace` (`boolean`, optional) - Whether to capture the stack trace or not.\n\n```js\nconst createError = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello')\nconsole.log(new CustomError()) // error.message => 'Hello'\n```\n\nHow to use an interpolated string:\n```js\nconst createError = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello %s')\nconsole.log(new CustomError('world')) // error.message => 'Hello world'\n```\n\nHow to add cause:\n```js\nconst createError = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello %s')\nconsole.log(new CustomError('world', {cause: new Error('cause')}))\n// error.message => 'Hello world'\n// error.cause => Error('cause')\n```\n\n### TypeScript\n\nIt is possible to limit your error constructor with a generic type using TypeScript:\n\n```ts\nconst CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')\nnew CustomError('world')\n//@ts-expect-error\nnew CustomError(1)\n```\n\n### instanceof\n\nAll errors created with `createError` will be instances of the base error constructor you provided, or `Error` if none was provided.\n\n```js\nconst createError = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)\nconst customError = new CustomError('world')\n\nconsole.log(customError instanceof CustomError) // true\nconsole.log(customError instanceof TypeError) // true\nconsole.log(customError instanceof Error) // true\n```\n\nAll instantiated errors are instances of the `FastifyError` class, which can be required directly from the module.\n\n```js\nconst { createError, FastifyError } = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)\nconst customError = new CustomError('world')\n\nconsole.log(customError instanceof FastifyError) // true\n```\n\nA `FastifyError` created by `createError` can extend another `FastifyError` while maintaining correct `instanceof` behavior.\n\n```js\nconst { createError, FastifyError } = require('@fastify/error')\n\nconst CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)\nconst ChildCustomError = createError('CHILD_ERROR_CODE', 'Hello %s', 500, CustomError)\n\nconst customError = new ChildCustomError('world')\n\nconsole.log(customError instanceof ChildCustomError) // true\nconsole.log(customError instanceof CustomError) // true\nconsole.log(customError instanceof FastifyError) // true\nconsole.log(customError instanceof TypeError) // true\nconsole.log(customError instanceof Error) // true\n```\n\nIf `fastify-error` is installed multiple times directly or as a transitive dependency, `instanceof` checks for errors created by `createError` will still work correctly across these installations, as long as their error codes (e.g., `FST_ERR_CUSTOM_ERROR`) are identical.\n\n```js\nconst { createError, FastifyError } = require('@fastify/error')\n\n// CustomError from `@fastify/some-plugin` is created with `createError` and\n// has its own `@fastify/error` installation as dependency. CustomError has\n// FST_ERR_CUSTOM_ERROR as code.\nconst { CustomError: CustomErrorFromPlugin } = require('@fastify/some-plugin')\n\nconst CustomError = createError('FST_ERR_CUSTOM_ERROR', 'Hello %s', 500)\n\nconst customError = new CustomError('world')\nconst customErrorFromPlugin = new CustomErrorFromPlugin('world')\n\nconsole.log(customError instanceof CustomError) // true\nconsole.log(customError instanceof CustomErrorFromPlugin) // true\nconsole.log(customErrorFromPlugin instanceof CustomError) // true\nconsole.log(customErrorFromPlugin instanceof CustomErrorFromPlugin) // true\n```\n\nChanging the code of an instantiated Error will not change the result of the `instanceof` operator.\n\n```js\nconst { createError, FastifyError } = require('@fastify/error')\n\nconst CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)\nconst AnotherCustomError = createError('ANOTHER_ERROR_CODE', 'Hello %s', 500, CustomError)\n\nconst customError = new CustomError('world')\ncustomError.code = 'ANOTHER_ERROR_CODE'\n\nconsole.log(customError instanceof CustomError) // true\nconsole.log(customError instanceof AnotherCustomError) // false\n```\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","publish_time":1741804742128,"_cnpm_publish_time":1741804742128,"dist-tags":{"latest":"4.2.1"}}