@fastify/error 2.0.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.
@@ -0,0 +1,13 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "monthly"
7
+ open-pull-requests-limit: 10
8
+
9
+ - package-ecosystem: "npm"
10
+ directory: "/"
11
+ schedule:
12
+ interval: "weekly"
13
+ open-pull-requests-limit: 10
@@ -0,0 +1,46 @@
1
+ name: CI
2
+ 'on':
3
+ push:
4
+ paths-ignore:
5
+ - docs/**
6
+ - '*.md'
7
+ pull_request:
8
+ paths-ignore:
9
+ - docs/**
10
+ - '*.md'
11
+ jobs:
12
+ 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 }}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Fastify
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # fastify-error
2
+
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)
6
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
+
8
+ A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
9
+
10
+ ### Install
11
+ ```
12
+ npm i fastify-error
13
+ ```
14
+
15
+ ### Usage
16
+
17
+ The module exports a function that you can use for consistent error objects, it takes 4 parameters:
18
+
19
+ ```
20
+ createError(code, message [, statusCode [, Base]])
21
+ ```
22
+
23
+ - `code` (`string`, required) - The error code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_`
24
+ - `message` (`string`, required) - The error message. You can also use interpolated strings for formatting the message.
25
+ - `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP.
26
+ - `Base` (`Error`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
27
+
28
+ ```js
29
+ const createError = require('fastify-error')
30
+ const CustomError = createError('ERROR_CODE', 'message')
31
+ console.log(new CustomError())
32
+ ```
33
+
34
+ How to use an interpolated string:
35
+ ```js
36
+ const createError = require('fastify-error')
37
+ const CustomError = createError('ERROR_CODE', 'Hello %s')
38
+ console.log(new CustomError('world')) // error.message => 'Hello world'
39
+ ```
40
+
41
+ ## License
42
+
43
+ Licensed under [MIT](./LICENSE).
package/index.d.ts ADDED
@@ -0,0 +1,22 @@
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/index.js ADDED
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+
3
+ const { inherits, format } = require('util')
4
+
5
+ function createError (code, message, statusCode = 500, Base = Error) {
6
+ if (!code) throw new Error('Fastify error code must not be empty')
7
+ if (!message) throw new Error('Fastify error message must not be empty')
8
+
9
+ code = code.toUpperCase()
10
+
11
+ function FastifyError (a, b, c) {
12
+ if (!new.target) {
13
+ return new FastifyError(...arguments)
14
+ }
15
+ Error.captureStackTrace(this, FastifyError)
16
+ this.name = 'FastifyError'
17
+ this.code = code
18
+
19
+ // more performant than spread (...) operator
20
+ switch (arguments.length) {
21
+ case 3:
22
+ this.message = format(message, a, b, c)
23
+ break
24
+ case 2:
25
+ this.message = format(message, a, b)
26
+ break
27
+ case 1:
28
+ this.message = format(message, a)
29
+ break
30
+ case 0:
31
+ this.message = message
32
+ break
33
+ default:
34
+ this.message = format(message, ...arguments)
35
+ }
36
+
37
+ this.statusCode = statusCode || undefined
38
+ }
39
+ FastifyError.prototype[Symbol.toStringTag] = 'Error'
40
+
41
+ FastifyError.prototype.toString = function () {
42
+ return `${this.name} [${this.code}]: ${this.message}`
43
+ }
44
+
45
+ inherits(FastifyError, Base)
46
+
47
+ return FastifyError
48
+ }
49
+
50
+ module.exports = createError
@@ -0,0 +1,10 @@
1
+ import createError, { FastifyError, FastifyErrorConstructor } from './'
2
+ import { expectType } from 'tsd'
3
+
4
+ const CustomError = createError('ERROR_CODE', 'message')
5
+ expectType<FastifyErrorConstructor>(CustomError)
6
+ const err = new CustomError()
7
+ expectType<FastifyError>(err)
8
+ expectType<string>(err.code)
9
+ expectType<string>(err.message)
10
+ expectType<number>(err.statusCode!)
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@fastify/error",
3
+ "version": "2.0.0",
4
+ "description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "test": "standard && ava -v && tsd"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/fastify/fastify-error.git"
13
+ },
14
+ "keywords": [
15
+ "fastify",
16
+ "error",
17
+ "utility",
18
+ "plugin"
19
+ ],
20
+ "author": "Tomas Della Vedova",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/fastify/fastify-error/issues"
24
+ },
25
+ "homepage": "https://github.com/fastify/fastify-error#readme",
26
+ "devDependencies": {
27
+ "@types/node": "^17.0.0",
28
+ "ava": "^3.9.0",
29
+ "standard": "^17.0.0",
30
+ "tsd": "^0.20.0",
31
+ "typescript": "^4.1.3"
32
+ },
33
+ "tsd": {
34
+ "compilerOptions": {
35
+ "esModuleInterop": true
36
+ }
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
41
+ }
package/test.js ADDED
@@ -0,0 +1,127 @@
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
+ })