2.3.19 • Published 12 days ago
@npmtuanmap/earum-vel-culpa-saepe v2.3.19
@npmtuanmap/earum-vel-culpa-saepe
A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
Install
npm i @npmtuanmap/earum-vel-culpa-saepe
Usage
The module exports a function that you can use for consistent error objects, it takes 4 parameters:
createError(code, message [, statusCode [, Base]])
code
(string
, required) - The error code, you can access it later witherror.code
. For consistency, we recommend prefixing plugin error codes withFST_
message
(string
, required) - The error message. You can also use interpolated strings for formatting the message.statusCode
(number
, optional) - The status code that Fastify will use if the error is sent via HTTP.Base
(ErrorConstructor
, optional) - The base error object that will be used. (egTypeError
,RangeError
)
const createError = require('@npmtuanmap/earum-vel-culpa-saepe')
const CustomError = createError('ERROR_CODE', 'Hello')
console.log(new CustomError()) // error.message => 'Hello'
How to use an interpolated string:
const createError = require('@npmtuanmap/earum-vel-culpa-saepe')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world')) // error.message => 'Hello world'
How to add cause:
const createError = require('@npmtuanmap/earum-vel-culpa-saepe')
const CustomError = createError('ERROR_CODE', 'Hello %s')
console.log(new CustomError('world', {cause: new Error('cause')}))
// error.message => 'Hello world'
// error.cause => Error('cause')
TypeScript
It is possible to limit your error constructor with a generic type using TypeScript:
const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
new CustomError('world')
//@ts-expect-error
new CustomError(1)
License
Licensed under MIT.
2.3.19
12 days ago
2.3.17
14 days ago
2.2.17
15 days ago
2.3.18
13 days ago
2.2.16
16 days ago
2.2.15
17 days ago
2.2.14
18 days ago
2.2.13
19 days ago
2.2.12
20 days ago
2.1.12
21 days ago
2.1.11
22 days ago
2.1.10
23 days ago
2.1.9
24 days ago
2.1.8
25 days ago
2.0.7
27 days ago
2.0.6
28 days ago
2.0.8
26 days ago
2.0.5
29 days ago
2.0.4
30 days ago
2.0.3
1 month ago
2.0.2
1 month ago
1.0.2
1 month ago
1.0.1
1 month ago