@fastify/error 2.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,46 +1,18 @@
1
1
  name: CI
2
- 'on':
2
+
3
+ on:
3
4
  push:
4
5
  paths-ignore:
5
- - docs/**
6
+ - 'docs/**'
6
7
  - '*.md'
7
8
  pull_request:
8
9
  paths-ignore:
9
- - docs/**
10
+ - 'docs/**'
10
11
  - '*.md'
12
+
11
13
  jobs:
12
14
  test:
13
- runs-on: ${{ matrix.os }}
14
- strategy:
15
- matrix:
16
- node-version:
17
- - 10
18
- - 12
19
- - 14
20
- - 16
21
- os:
22
- - macos-latest
23
- - ubuntu-latest
24
- - windows-latest
25
- steps:
26
- - uses: actions/checkout@v3
27
- - name: Use Node.js
28
- uses: actions/setup-node@v3
29
- with:
30
- node-version: ${{ matrix.node-version }}
31
- - name: Install Dependencies
32
- run: |
33
- npm install --ignore-scripts
34
- - name: Run Tests
35
- run: |
36
- npm test
37
- automerge:
38
- needs: test
39
- runs-on: ubuntu-latest
40
- permissions:
41
- pull-requests: write
42
- contents: write
43
- steps:
44
- - uses: fastify/github-action-merge-dependabot@v3
45
- with:
46
- github-token: ${{ secrets.GITHUB_TOKEN }}
15
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
+ with:
17
+ license-check: true
18
+ lint: true
package/.taprc ADDED
@@ -0,0 +1,2 @@
1
+ files:
2
+ - test/**/*.test.js
package/README.md CHANGED
@@ -1,15 +1,14 @@
1
- # fastify-error
1
+ # @fastify/error
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-error/workflows/CI/badge.svg)
4
- [![NPM version](https://img.shields.io/npm/v/fastify-error.svg?style=flat)](https://www.npmjs.com/package/fastify-error)
5
- [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-error/badge.svg)](https://snyk.io/test/github/fastify/fastify-error)
4
+ [![NPM version](https://img.shields.io/npm/v/@fastify/error.svg?style=flat)](https://www.npmjs.com/package/@fastify/error)
6
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
6
 
8
7
  A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
9
8
 
10
9
  ### Install
11
10
  ```
12
- npm i fastify-error
11
+ npm i @fastify/error
13
12
  ```
14
13
 
15
14
  ### Usage
@@ -26,14 +25,14 @@ createError(code, message [, statusCode [, Base]])
26
25
  - `Base` (`Error`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
27
26
 
28
27
  ```js
29
- const createError = require('fastify-error')
28
+ const createError = require('@fastify/error')
30
29
  const CustomError = createError('ERROR_CODE', 'message')
31
30
  console.log(new CustomError())
32
31
  ```
33
32
 
34
33
  How to use an interpolated string:
35
34
  ```js
36
- const createError = require('fastify-error')
35
+ const createError = require('@fastify/error')
37
36
  const CustomError = createError('ERROR_CODE', 'Hello %s')
38
37
  console.log(new CustomError('world')) // error.message => 'Hello world'
39
38
  ```
package/index.js CHANGED
@@ -48,3 +48,5 @@ function createError (code, message, statusCode = 500, Base = Error) {
48
48
  }
49
49
 
50
50
  module.exports = createError
51
+ module.exports.default = createError
52
+ module.exports.createError = createError
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@fastify/error",
3
- "version": "2.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
5
5
  "main": "index.js",
6
- "types": "index.d.ts",
6
+ "types": "types/index.d.ts",
7
7
  "scripts": {
8
- "test": "standard && ava -v && tsd"
8
+ "lint": "standard",
9
+ "test": "npm run test:unit && npm run test:typescript",
10
+ "test:unit": "tap",
11
+ "test:typescript": "tsd"
9
12
  },
10
13
  "repository": {
11
14
  "type": "git",
@@ -24,11 +27,9 @@
24
27
  },
25
28
  "homepage": "https://github.com/fastify/fastify-error#readme",
26
29
  "devDependencies": {
27
- "@types/node": "^17.0.0",
28
- "ava": "^3.9.0",
29
30
  "standard": "^17.0.0",
30
- "tsd": "^0.20.0",
31
- "typescript": "^4.1.3"
31
+ "tap": "^16.0.0",
32
+ "tsd": "^0.24.1"
32
33
  },
33
34
  "tsd": {
34
35
  "compilerOptions": {
@@ -0,0 +1,156 @@
1
+ 'use strict'
2
+
3
+ /* eslint no-prototype-builtins: 0 */
4
+
5
+ const test = require('tap').test
6
+ const createError = require('..')
7
+
8
+ test('Create error with zero parameter', t => {
9
+ t.plan(6)
10
+
11
+ const NewError = createError('CODE', 'Not available')
12
+ const err = new NewError()
13
+ t.ok(err instanceof Error)
14
+ t.equal(err.name, 'FastifyError')
15
+ t.equal(err.message, 'Not available')
16
+ t.equal(err.code, 'CODE')
17
+ t.equal(err.statusCode, 500)
18
+ t.ok(err.stack)
19
+ })
20
+
21
+ test('Create error with 1 parameter', t => {
22
+ t.plan(6)
23
+
24
+ const NewError = createError('CODE', 'hey %s')
25
+ const err = new NewError('alice')
26
+ t.ok(err instanceof Error)
27
+ t.equal(err.name, 'FastifyError')
28
+ t.equal(err.message, 'hey alice')
29
+ t.equal(err.code, 'CODE')
30
+ t.equal(err.statusCode, 500)
31
+ t.ok(err.stack)
32
+ })
33
+
34
+ test('Create error with 1 parameter set to undefined', t => {
35
+ t.plan(1)
36
+
37
+ const NewError = createError('CODE', 'hey %s')
38
+ const err = new NewError(undefined)
39
+ t.equal(err.message, 'hey undefined')
40
+ })
41
+
42
+ test('Create error with 2 parameters', (t) => {
43
+ t.plan(6)
44
+
45
+ const NewError = createError('CODE', 'hey %s, I like your %s')
46
+ const err = new NewError('alice', 'attitude')
47
+ t.ok(err instanceof Error)
48
+ t.equal(err.name, 'FastifyError')
49
+ t.equal(err.message, 'hey alice, I like your attitude')
50
+ t.equal(err.code, 'CODE')
51
+ t.equal(err.statusCode, 500)
52
+ t.ok(err.stack)
53
+ })
54
+
55
+ test('Create error with 2 parameters set to undefined', t => {
56
+ t.plan(1)
57
+
58
+ const NewError = createError('CODE', 'hey %s, I like your %s')
59
+ const err = new NewError(undefined, undefined)
60
+ t.equal(err.message, 'hey undefined, I like your undefined')
61
+ })
62
+
63
+ test('Create error with 3 parameters', t => {
64
+ t.plan(6)
65
+
66
+ const NewError = createError('CODE', 'hey %s, I like your %s %s')
67
+ const err = new NewError('alice', 'attitude', 'see you')
68
+ t.ok(err instanceof Error)
69
+ t.equal(err.name, 'FastifyError')
70
+ t.equal(err.message, 'hey alice, I like your attitude see you')
71
+ t.equal(err.code, 'CODE')
72
+ t.equal(err.statusCode, 500)
73
+ t.ok(err.stack)
74
+ })
75
+
76
+ test('Create error with 3 parameters set to undefined', t => {
77
+ t.plan(4)
78
+
79
+ const NewError = createError('CODE', 'hey %s, I like your %s %s')
80
+ const err = new NewError(undefined, undefined, undefined)
81
+ t.equal(err.message, 'hey undefined, I like your undefined undefined')
82
+ t.equal(err.code, 'CODE')
83
+ t.equal(err.statusCode, 500)
84
+ t.ok(err.stack)
85
+ })
86
+
87
+ test('Create error with 4 parameters set to undefined', t => {
88
+ t.plan(4)
89
+
90
+ const NewError = createError('CODE', 'hey %s, I like your %s %s and %s')
91
+ const err = new NewError(undefined, undefined, undefined, undefined)
92
+ t.equal(err.message, 'hey undefined, I like your undefined undefined and undefined')
93
+ t.equal(err.code, 'CODE')
94
+ t.equal(err.statusCode, 500)
95
+ t.ok(err.stack)
96
+ })
97
+
98
+ test('Create error with no statusCode property', t => {
99
+ t.plan(6)
100
+
101
+ const NewError = createError('CODE', 'hey %s', 0)
102
+ const err = new NewError('dude')
103
+ t.ok(err instanceof Error)
104
+ t.equal(err.name, 'FastifyError')
105
+ t.equal(err.message, 'hey dude')
106
+ t.equal(err.code, 'CODE')
107
+ t.equal(err.statusCode, undefined)
108
+ t.ok(err.stack)
109
+ })
110
+
111
+ test('Should throw when error code has no fastify code', t => {
112
+ t.plan(1)
113
+
114
+ t.throws(() => createError(), new Error('Fastify error code must not be empty'))
115
+ })
116
+
117
+ test('Should throw when error code has no message', t => {
118
+ t.plan(1)
119
+
120
+ t.throws(() => createError('code'), new Error('Fastify error message must not be empty'))
121
+ })
122
+
123
+ test('Create error with different base', t => {
124
+ t.plan(7)
125
+
126
+ const NewError = createError('CODE', 'hey %s', 500, TypeError)
127
+ const err = new NewError('dude')
128
+ t.ok(err instanceof Error)
129
+ t.ok(err instanceof TypeError)
130
+ t.equal(err.name, 'FastifyError')
131
+ t.equal(err.message, 'hey dude')
132
+ t.equal(err.code, 'CODE')
133
+ t.equal(err.statusCode, 500)
134
+ t.ok(err.stack)
135
+ })
136
+
137
+ test('FastifyError.toString returns code', t => {
138
+ t.plan(1)
139
+
140
+ const NewError = createError('CODE', 'foo')
141
+ const err = new NewError()
142
+ t.equal(err.toString(), 'FastifyError [CODE]: foo')
143
+ })
144
+
145
+ test('Create the error without the new keyword', t => {
146
+ t.plan(6)
147
+
148
+ const NewError = createError('CODE', 'Not available')
149
+ const err = NewError()
150
+ t.ok(err instanceof Error)
151
+ t.equal(err.name, 'FastifyError')
152
+ t.equal(err.message, 'Not available')
153
+ t.equal(err.code, 'CODE')
154
+ t.equal(err.statusCode, 500)
155
+ t.ok(err.stack)
156
+ })
@@ -0,0 +1,21 @@
1
+ type CreateError = (code: string, message: string, statusCode?: number, Base?: Error) => createError.FastifyErrorConstructor
2
+
3
+ declare namespace createError {
4
+ export interface FastifyError extends Error {
5
+ code: string;
6
+ name: string;
7
+ statusCode?: number;
8
+ }
9
+
10
+ export interface FastifyErrorConstructor {
11
+ new(a?: any, b?: any, c?: any): FastifyError;
12
+ (a?: any, b?: any, c?: any): FastifyError;
13
+ readonly prototype: FastifyError;
14
+ }
15
+
16
+ export const createError: CreateError
17
+ export { createError as default }
18
+ }
19
+
20
+ declare function createError(...params: Parameters<CreateError>): ReturnType<CreateError>
21
+ export = createError
@@ -1,4 +1,4 @@
1
- import createError, { FastifyError, FastifyErrorConstructor } from './'
1
+ import createError, { FastifyError, FastifyErrorConstructor } from '..'
2
2
  import { expectType } from 'tsd'
3
3
 
4
4
  const CustomError = createError('ERROR_CODE', 'message')
package/index.d.ts DELETED
@@ -1,22 +0,0 @@
1
- export = createError
2
-
3
- declare function createError (
4
- code: string,
5
- message: string,
6
- statusCode?: number,
7
- Base?: Error
8
- ): createError.FastifyErrorConstructor
9
-
10
- declare namespace createError {
11
- interface FastifyError extends Error {
12
- code: string;
13
- name: string;
14
- statusCode?: number;
15
- }
16
-
17
- interface FastifyErrorConstructor {
18
- new(a?: any, b?: any, c?: any): FastifyError;
19
- (a?: any, b?: any, c?: any): FastifyError;
20
- readonly prototype: FastifyError;
21
- }
22
- }
package/test.js DELETED
@@ -1,127 +0,0 @@
1
- 'use strict'
2
-
3
- /* eslint no-prototype-builtins: 0 */
4
-
5
- const test = require('ava')
6
- const createError = require('./')
7
-
8
- test('Create error with zero parameter', t => {
9
- const NewError = createError('CODE', 'Not available')
10
- const err = new NewError()
11
- t.true(err instanceof Error)
12
- t.is(err.name, 'FastifyError')
13
- t.is(err.message, 'Not available')
14
- t.is(err.code, 'CODE')
15
- t.is(err.statusCode, 500)
16
- t.truthy(err.stack)
17
- })
18
-
19
- test('Create error with 1 parameter', t => {
20
- const NewError = createError('CODE', 'hey %s')
21
- const err = new NewError('alice')
22
- t.true(err instanceof Error)
23
- t.is(err.name, 'FastifyError')
24
- t.is(err.message, 'hey alice')
25
- t.is(err.code, 'CODE')
26
- t.is(err.statusCode, 500)
27
- t.truthy(err.stack)
28
- })
29
-
30
- test('Create error with 1 parameter set to undefined', t => {
31
- const NewError = createError('CODE', 'hey %s')
32
- const err = new NewError(undefined)
33
- t.is(err.message, 'hey undefined')
34
- })
35
-
36
- test('Create error with 2 parameters', (t) => {
37
- const NewError = createError('CODE', 'hey %s, I like your %s')
38
- const err = new NewError('alice', 'attitude')
39
- t.true(err instanceof Error)
40
- t.is(err.name, 'FastifyError')
41
- t.is(err.message, 'hey alice, I like your attitude')
42
- t.is(err.code, 'CODE')
43
- t.is(err.statusCode, 500)
44
- t.truthy(err.stack)
45
- })
46
-
47
- test('Create error with 2 parameters set to undefined', t => {
48
- const NewError = createError('CODE', 'hey %s, I like your %s')
49
- const err = new NewError(undefined, undefined)
50
- t.is(err.message, 'hey undefined, I like your undefined')
51
- })
52
-
53
- test('Create error with 3 parameters', t => {
54
- const NewError = createError('CODE', 'hey %s, I like your %s %s')
55
- const err = new NewError('alice', 'attitude', 'see you')
56
- t.true(err instanceof Error)
57
- t.is(err.name, 'FastifyError')
58
- t.is(err.message, 'hey alice, I like your attitude see you')
59
- t.is(err.code, 'CODE')
60
- t.is(err.statusCode, 500)
61
- t.truthy(err.stack)
62
- })
63
-
64
- test('Create error with 3 parameters set to undefined', t => {
65
- const NewError = createError('CODE', 'hey %s, I like your %s %s')
66
- const err = new NewError(undefined, undefined, undefined)
67
- t.is(err.message, 'hey undefined, I like your undefined undefined')
68
- t.is(err.code, 'CODE')
69
- t.is(err.statusCode, 500)
70
- t.truthy(err.stack)
71
- })
72
-
73
- test('Create error with no statusCode property', t => {
74
- const NewError = createError('CODE', 'hey %s', 0)
75
- const err = new NewError('dude')
76
- t.true(err instanceof Error)
77
- t.is(err.name, 'FastifyError')
78
- t.is(err.message, 'hey dude')
79
- t.is(err.code, 'CODE')
80
- t.is(err.statusCode, undefined)
81
- t.truthy(err.stack)
82
- })
83
-
84
- test('Should throw when error code has no fastify code', t => {
85
- try {
86
- createError()
87
- } catch (err) {
88
- t.is(err.message, 'Fastify error code must not be empty')
89
- }
90
- })
91
-
92
- test('Should throw when error code has no message', t => {
93
- try {
94
- createError('code')
95
- } catch (err) {
96
- t.is(err.message, 'Fastify error message must not be empty')
97
- }
98
- })
99
-
100
- test('Create error with different base', t => {
101
- const NewError = createError('CODE', 'hey %s', 500, TypeError)
102
- const err = new NewError('dude')
103
- t.true(err instanceof Error)
104
- t.true(err instanceof TypeError)
105
- t.is(err.name, 'FastifyError')
106
- t.is(err.message, 'hey dude')
107
- t.is(err.code, 'CODE')
108
- t.is(err.statusCode, 500)
109
- t.truthy(err.stack)
110
- })
111
-
112
- test('FastifyError.toString returns code', t => {
113
- const NewError = createError('CODE', 'foo')
114
- const err = new NewError()
115
- t.is(err.toString(), 'FastifyError [CODE]: foo')
116
- })
117
-
118
- test('Create the error without the new keyword', t => {
119
- const NewError = createError('CODE', 'Not available')
120
- const err = NewError()
121
- t.true(err instanceof Error)
122
- t.is(err.name, 'FastifyError')
123
- t.is(err.message, 'Not available')
124
- t.is(err.code, 'CODE')
125
- t.is(err.statusCode, 500)
126
- t.truthy(err.stack)
127
- })