Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/error

Package Overview
Dependencies
Maintainers
17
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/error - npm Package Compare versions

Comparing version 3.2.0 to 3.2.1

benchmarks/create.js

30

index.js

@@ -10,31 +10,17 @@ 'use strict'

code = code.toUpperCase()
!statusCode && (statusCode = undefined)
function FastifyError (a, b, c) {
function FastifyError (...args) {
if (!new.target) {
return new FastifyError(...arguments)
return new FastifyError(...args)
}
Error.captureStackTrace(this, FastifyError)
this.code = code
this.name = 'FastifyError'
this.code = code
this.statusCode = statusCode
// more performant than spread (...) operator
switch (arguments.length) {
case 3:
this.message = format(message, a, b, c)
break
case 2:
this.message = format(message, a, b)
break
case 1:
this.message = format(message, a)
break
case 0:
this.message = message
break
default:
this.message = format(message, ...arguments)
}
this.message = format(message, ...args)
this.statusCode = statusCode || undefined
Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
}
FastifyError.prototype[Symbol.toStringTag] = 'Error'

@@ -41,0 +27,0 @@

{
"name": "@fastify/error",
"version": "3.2.0",
"version": "3.2.1",
"description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",

@@ -9,2 +9,3 @@ "main": "index.js",

"lint": "standard",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",

@@ -31,5 +32,6 @@ "test:unit": "tap",

"devDependencies": {
"benchmark": "^2.1.4",
"standard": "^17.0.0",
"tap": "^16.0.0",
"tsd": "^0.25.0"
"tsd": "^0.28.0"
},

@@ -36,0 +38,0 @@ "tsd": {

@@ -40,4 +40,17 @@ # @fastify/error

### TypeScript
It is possible to limit your error constructor with a generic type using TypeScript:
```ts
const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
new CustomError('world')
//@ts-expect-error
new CustomError(1)
//@ts-expect-error
new CustomError(1)
```
## License
Licensed under [MIT](./LICENSE).

@@ -1,17 +0,38 @@

declare function createError<C extends string, SC extends number>(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>;
declare function createError<C extends string>(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>;
declare function createError<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> (
code: C,
message: string,
statusCode: SC,
Base?: Error
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
type CreateError = typeof createError;
declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
code: C,
message: string,
statusCode?: number,
Base?: Error
): createError.FastifyErrorConstructor<{ code: C }, Arg>
declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
code: string,
message: string,
statusCode?: number,
Base?: Error
): createError.FastifyErrorConstructor<{ code: string }, Arg>
type CreateError = typeof createError
declare namespace createError {
export interface FastifyError extends Error {
code: string;
name: string;
statusCode?: number;
code: string
name: string
statusCode?: number
}
export interface FastifyErrorConstructor<E extends { code: string; statusCode?: number } = { code: string; statusCode?: number }> {
new (a?: any, b?: any, c?: any): FastifyError & E;
(a?: any, b?: any, c?: any): FastifyError & E;
readonly prototype: FastifyError & E;
export interface FastifyErrorConstructor<
E extends { code: string, statusCode?: number } = { code: string, statusCode?: number },
T extends unknown[] = [any?, any?, any?]
> {
new(...arg: T): FastifyError & E
(...arg: T): FastifyError & E
readonly prototype: FastifyError & E
}

@@ -18,0 +39,0 @@

import createError, { FastifyError, FastifyErrorConstructor } from '..'
import { expectType } from 'tsd'
import { expectType, expectError } from 'tsd'

@@ -10,10 +10,59 @@ const CustomError = createError('ERROR_CODE', 'message')

expectType<string>(err.message)
expectType<number>(err.statusCode!)
expectType<number | undefined>(err.statusCode)
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>>(CustomTypedError)
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
const typed = new CustomTypedError()
expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
expectType<'OTHER_CODE'>(typed.code)
expectType<string>(typed.message)
expectType<400>(typed.statusCode)
/* eslint-disable no-new */
const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400)
CustomTypedArgError('a')
expectError(CustomTypedArgError('a', 'b'))
expectError(new CustomTypedArgError('a', 'b'))
expectError(CustomTypedArgError(1))
expectError(new CustomTypedArgError(1))
const CustomTypedArgError2 = createError<string, number, [string]>('OTHER_CODE', 'expect %s message', 400)
CustomTypedArgError2('a')
expectError(CustomTypedArgError2('a', 'b'))
expectError(new CustomTypedArgError2('a', 'b'))
expectError(CustomTypedArgError2(1))
expectError(new CustomTypedArgError2(1))
const CustomTypedArgError3 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
expectError(CustomTypedArgError3('a'))
CustomTypedArgError3('a', 'b')
new CustomTypedArgError3('a', 'b')
expectError(CustomTypedArgError3(1))
expectError(new CustomTypedArgError3(1))
expectError(new CustomTypedArgError3(1, 2))
expectError(new CustomTypedArgError3('1', 2))
expectError(new CustomTypedArgError3(1, '2'))
const CustomTypedArgError4 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
expectError(CustomTypedArgError4('a'))
CustomTypedArgError4('a', 'b')
new CustomTypedArgError4('a', 'b')
expectError(CustomTypedArgError4(1))
expectError(new CustomTypedArgError4(1))
expectError(new CustomTypedArgError4(1, 2))
expectError(new CustomTypedArgError4('1', 2))
expectError(new CustomTypedArgError4(1, '2'))
const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
expectError(CustomTypedArgError5('a'))
expectError(new CustomTypedArgError5('a', 'b'))
expectError(new CustomTypedArgError5('a', 'b', 'c'))
CustomTypedArgError5('a', 'b', 'c', 'd')
expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e'))
const CustomTypedArgError6 = createError<string, number, [string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
expectError(CustomTypedArgError6('a'))
expectError(new CustomTypedArgError6('a', 'b'))
expectError(new CustomTypedArgError6('a', 'b', 'c'))
CustomTypedArgError6('a', 'b', 'c', 'd')
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc