{"_id":"@fastify/error@4.2.1","bugs":{"url":"https://github.com/fastify/fastify-error/issues"},"dist":{"shasum":"6ec02a481559abc5ac8fd478f8a1f9391ee8188e","tarball":"http://registry-npm.ywwl.com/@fastify/error/download/@fastify/error-4.2.1.tgz","fileCount":18,"integrity":"sha512-MYBumuIwP+5MIpXSyKm6/o1Eigcoij186lWUssX3LH5U8KWqU0ktZhzC4CErIX9EJeQeRGI+zJZQWakBlFbUcg==","signatures":[{"sig":"MEUCICWzb9FRqzxZ02iTAwir6D2qfMAac7PzGCM7KyAtAsszAiEAzjSkkjaKdROtUHWLEQb2nyziFC7kInBeX/hSxklyPng=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDNVWwFYYuTfPj+COg/XrmdguweTrLIUsxxrjASpNJp7wIgCws1DB4IqGClGZVAaLgyJ+2RRddZz/Ws/5+Y16lSd9M="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@fastify%2ferror@4.2.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":40081,"size":8794,"noattachment":false,"key":"/@fastify/error/-/@fastify/error-4.2.1.tgz"},"main":"index.js","name":"@fastify/error","type":"commonjs","types":"types/index.d.ts","author":{"name":"Tomas Della Vedova"},"funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"9e864fdbadb0ae0701ae536f9725a3456aa7bfac","license":"MIT","scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"c8 --100 node --test","test:typescript":"tstyche"},"version":"4.2.1","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"8e1e6053-e2a3-469b-ab36-922ac8d895bf"}},"homepage":"https://github.com/fastify/fastify-error#readme","keywords":["fastify","error","utility","plugin"],"repository":{"url":"git+https://github.com/fastify/fastify-error.git","type":"git"},"_npmVersion":"11.19.0","description":"A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.","directories":{},"maintainers":[{"name":"eomm","email":"behemoth89@gmail.com"}],"_nodeVersion":"24.21.0","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"}],"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^12.0.0","eslint":"^9.17.0","tstyche":"^7.1.0","benchmark":"^2.1.4","neostandard":"^0.13.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/error_4.2.1_1791627244539_0.21029268933523482"},"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":1791627244615,"_cnpm_publish_time":1791627244615,"dist-tags":{"latest":"4.2.1"}}