Gathering detailed insights and metrics for fastify-better-error
Gathering detailed insights and metrics for fastify-better-error
Gathering detailed insights and metrics for fastify-better-error
Gathering detailed insights and metrics for fastify-better-error
Supercharge Fastify error handling! Streamline definitions, automate schemas, and boost productivity with built-in HTTP errors and robust TypeScript support. Make errors work for you!
npm install fastify-better-error
Typescript
Module System
Node Version
NPM Version
74.9
Supply Chain
99
Quality
82.9
Maintenance
100
Vulnerability
100
License
TypeScript (99.61%)
JavaScript (0.39%)
Total Downloads
2,170
Last Day
3
Last Week
38
Last Month
183
Last Year
2,170
MIT License
27 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Apr 04, 2025
Minified
Minified + Gzipped
Latest Version
2.0.2
Package Id
fastify-better-error@2.0.2
Unpacked Size
98.17 kB
Size
13.51 kB
File Count
23
NPM Version
10.9.2
Node Version
22.14.0
Published on
Apr 04, 2025
Cumulative downloads
Total Downloads
Last Day
-82.4%
3
Compared to previous day
Last Week
-11.6%
38
Compared to previous week
Last Month
-54.9%
183
Compared to previous month
Last Year
0%
2,170
Compared to previous year
2
Supercharge Fastify error handling! Streamline definitions, automate schemas, and boost productivity with built-in HTTP errors and robust TypeScript support. Make errors work for you!
This plugin simplifies how to define and use errors in Fastify. It provides a way to create custom errors with specific status codes and messages, and to automatically generate response schemas for these errors.
Additionally, it includes a comprehensive set of predefined HTTP errors and provides robust TypeScript support with enhanced IntelliSense capabilities. This combination allows for more efficient error handling and improved developer experience through better code completion and type checking.
1$ npm install fastify-better-error
Using JavaScript with JSDoc:
1/** 2 * @import { BetterErrorPlugin } from 'fastify-better-error' 3 */ 4 5import fastify from 'fastify' 6import betterErrorPlugin, { createError } from 'fastify-better-error' 7 8const CUSTOM_ERRORS = { 9 WrongCredentialsError: createError(401, 'WRONG_CREDENTIALS', 'Wrong credentials: %s'), 10} 11 12const baseApp = fastify() 13 14// By doing this, we are telling TypeScript that `app` has all the 15// properties of `baseApp` plus the ones defined in `BetterErrorPlugin`. 16const app = /** @type {typeof baseApp & BetterErrorPlugin<typeof CUSTOM_ERRORS>} */(baseApp) 17 18await app.register(betterErrorPlugin, { 19 errors: CUSTOM_ERRORS, 20}) 21 22app.get('/', { 23 schema: { 24 response: { 25 // it will automatically generate the 401 and 404 status code for this error 26 ...app.useErrors([ 27 'WrongCredentialsError', 28 'NotFoundError', 29 ]), 30 }, 31 }, 32}, (req) => { 33 if (!(req.token)) { 34 throw new app.errors.WrongCredentialsError('missing token') 35 } 36 37 if (!(req.user)) { 38 throw new app.errors.NotFoundError('user not found') 39 } 40 41 return {} 42})
Using TypeScript:
1import fastify from 'fastify' 2import betterErrorPlugin, { type BetterErrorPlugin, createError } from 'fastify-better-error' 3 4const CUSTOM_ERRORS = { 5 WrongCredentialsError: createError(401, 'WRONG_CREDENTIALS', 'Wrong credentials: %s'), 6} 7 8const baseApp = fastify() 9 10const app = baseApp as typeof baseApp & BetterErrorPlugin<typeof CUSTOM_ERRORS> 11 12await app.register(betterErrorPlugin, { 13 errors: CUSTOM_ERRORS, 14}) 15 16app.get('/', { 17 schema: { 18 response: { 19 // it will automatically generate the 401 and 404 status code for this error 20 ...app.useErrors([ 21 'WrongCredentialsError', 22 'NotFoundError', 23 ]), 24 }, 25 }, 26}, (req) => { 27 if (!(req.token)) { 28 throw new app.errors.WrongCredentialsError('missing token') 29 } 30 31 if (!(req.user)) { 32 throw new app.errors.NotFoundError('user not found') 33 } 34 35 return {} 36})
The plugin also includes the default HTTP errors:
:bug: If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.
:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this guideline.
MIT © 2024 Martin Acosta
No vulnerabilities found.
No security vulnerabilities found.