{"id":14969174,"url":"https://github.com/fastify/fastify-error","last_synced_at":"2025-05-15T01:08:26.215Z","repository":{"id":37958752,"uuid":"273911567","full_name":"fastify/fastify-error","owner":"fastify","description":"A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins","archived":false,"fork":false,"pushed_at":"2025-05-01T16:22:25.000Z","size":123,"stargazers_count":120,"open_issues_count":5,"forks_count":19,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-05-07T04:05:58.455Z","etag":null,"topics":["fastify","fastify-library"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/error","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2020-06-21T13:38:33.000Z","updated_at":"2025-05-05T16:43:39.000Z","dependencies_parsed_at":"2024-01-25T20:02:12.227Z","dependency_job_id":"d32dca19-5c73-4cf7-a830-69f1d4defd3d","html_url":"https://github.com/fastify/fastify-error","commit_stats":{"total_commits":120,"total_committers":23,"mean_commits":5.217391304347826,"dds":0.5833333333333333,"last_synced_commit":"d06ad6a9a80b75dbd6f22eb86be94b588a9a1dde"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-error/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254042,"owners_count":22039792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-library"],"created_at":"2024-09-24T13:41:17.192Z","updated_at":"2025-05-15T01:08:21.205Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/error\n\n[![CI](https://github.com/fastify/fastify-error/actions/workflows/ci.yml/badge.svg?branch=master)](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```\ncreateError(code, message [, statusCode [, Base]])\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\n```js\nconst createError = require('@fastify/error')\nconst CustomError = createError('ERROR_CODE', 'Hello')\nconsole.log(new CustomError()) // error.message =\u003e '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 =\u003e '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 =\u003e 'Hello world'\n// error.cause =\u003e 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\u003c[string]\u003e('ERROR_CODE', 'Hello %s')\nnew CustomError('world')\n//@ts-expect-error\nnew CustomError(1)\n```\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-error/lists"}