@fastify/error
Advanced tools
Comparing version 3.3.0 to 3.4.0
@@ -10,2 +10,3 @@ 'use strict' | ||
const cause = new Error('cause') | ||
new benchmark.Suite() | ||
@@ -16,3 +17,4 @@ .add('instantiate Error', function () { new Error() }, { minSamples: 100 }) // eslint-disable-line no-new | ||
.add('instantiate FastifyError arg 2', function () { new FastifyError2('qq', 'ss') }, { minSamples: 100 }) // eslint-disable-line no-new | ||
.add('instantiate FastifyError cause', function () { new FastifyError2({ cause }) }, { minSamples: 100 }) // eslint-disable-line no-new | ||
.on('cycle', function onCycle (event) { console.log(String(event.target)) }) | ||
.run({ async: false }) |
'use strict' | ||
const { format } = require('util') | ||
const { format } = require('node:util') | ||
@@ -20,2 +20,3 @@ function toString () { | ||
} | ||
this.code = code | ||
@@ -25,2 +26,7 @@ this.name = 'FastifyError' | ||
const lastElement = args.length - 1 | ||
if (lastElement !== 1 && args[lastElement] && typeof args[lastElement] === 'object' && 'cause' in args[lastElement]) { | ||
this.cause = args.pop().cause | ||
} | ||
this.message = format(message, ...args) | ||
@@ -27,0 +33,0 @@ |
{ | ||
"name": "@fastify/error", | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.28.0" | ||
"tsd": "^0.29.0" | ||
}, | ||
@@ -37,0 +37,0 @@ "tsd": { |
@@ -25,8 +25,8 @@ # @fastify/error | ||
- `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP. | ||
- `Base` (`Error`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`) | ||
- `Base` (`ErrorConstructor`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`) | ||
```js | ||
const createError = require('@fastify/error') | ||
const CustomError = createError('ERROR_CODE', 'message') | ||
console.log(new CustomError()) | ||
const CustomError = createError('ERROR_CODE', 'Hello') | ||
console.log(new CustomError()) // error.message => 'Hello' | ||
``` | ||
@@ -41,2 +41,11 @@ | ||
How to add cause: | ||
```js | ||
const createError = require('@fastify/error') | ||
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 | ||
@@ -51,4 +60,2 @@ | ||
new CustomError(1) | ||
//@ts-expect-error | ||
new CustomError(1) | ||
``` | ||
@@ -55,0 +62,0 @@ |
'use strict' | ||
/* eslint no-prototype-builtins: 0 */ | ||
const test = require('tap').test | ||
@@ -157,1 +155,21 @@ const createError = require('..') | ||
}) | ||
test('Create an error with cause', t => { | ||
t.plan(2) | ||
const cause = new Error('HEY') | ||
const NewError = createError('CODE', 'Not available') | ||
const err = NewError({ cause }) | ||
t.ok(err instanceof Error) | ||
t.equal(err.cause, cause) | ||
}) | ||
test('Create an error with last argument null', t => { | ||
t.plan(2) | ||
const cause = new Error('HEY') | ||
const NewError = createError('CODE', 'Not available') | ||
const err = NewError({ cause }, null) | ||
t.ok(err instanceof Error) | ||
t.notOk(err.cause) | ||
}) |
@@ -5,3 +5,3 @@ declare function createError<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> ( | ||
statusCode: SC, | ||
Base?: Error | ||
Base?: ErrorConstructor | ||
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg> | ||
@@ -13,3 +13,3 @@ | ||
statusCode?: number, | ||
Base?: Error | ||
Base?: ErrorConstructor | ||
): createError.FastifyErrorConstructor<{ code: C }, Arg> | ||
@@ -21,3 +21,3 @@ | ||
statusCode?: number, | ||
Base?: Error | ||
Base?: ErrorConstructor | ||
): createError.FastifyErrorConstructor<{ code: string }, Arg> | ||
@@ -24,0 +24,0 @@ |
@@ -68,1 +68,6 @@ import createError, { FastifyError, FastifyErrorConstructor } from '..' | ||
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e')) | ||
const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError) | ||
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor) | ||
CustomErrorWithErrorConstructor({cause: new Error('Error')}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18096
318
63