1 | 'use strict'
|
2 |
|
3 | const { inherits, format } = require('util')
|
4 | const codes = {}
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | createError('FST_ERR_NOT_FOUND', 'Not Found', 404)
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | createError('FST_ERR_CTP_ALREADY_PRESENT', "Content type parser '%s' already present.")
|
16 | createError('FST_ERR_CTP_INVALID_TYPE', 'The content type should be a string', 500, TypeError)
|
17 | createError('FST_ERR_CTP_EMPTY_TYPE', 'The content type cannot be an empty string', 500, TypeError)
|
18 | createError('FST_ERR_CTP_INVALID_HANDLER', 'The content type handler should be a function', 500, TypeError)
|
19 | createError('FST_ERR_CTP_INVALID_PARSE_TYPE', "The body parser can only parse your data as 'string' or 'buffer', you asked '%s' which is not supported.", 500, TypeError)
|
20 | createError('FST_ERR_CTP_BODY_TOO_LARGE', 'Request body is too large', 413, RangeError)
|
21 | createError('FST_ERR_CTP_INVALID_MEDIA_TYPE', 'Unsupported Media Type: %s', 415)
|
22 | createError('FST_ERR_CTP_INVALID_CONTENT_LENGTH', 'Request body size did not match Content-Length', 400, RangeError)
|
23 | createError('FST_ERR_CTP_EMPTY_JSON_BODY', "Body cannot be empty when content-type is set to 'application/json'", 400)
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | createError('FST_ERR_DEC_ALREADY_PRESENT', "The decorator '%s' has already been added!")
|
29 | createError('FST_ERR_DEC_MISSING_DEPENDENCY', "The decorator is missing dependency '%s'.")
|
30 | createError('FST_ERR_DEC_AFTER_START', "The decorator '%s' has been added after start!")
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | createError('FST_ERR_HOOK_INVALID_TYPE', 'The hook name must be a string', 500, TypeError)
|
36 | createError('FST_ERR_HOOK_INVALID_HANDLER', 'The hook callback must be a function', 500, TypeError)
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | createError('FST_ERR_LOG_INVALID_DESTINATION', 'Cannot specify both logger.stream and logger.file options')
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | createError('FST_ERR_REP_INVALID_PAYLOAD_TYPE', "Attempted to send payload of invalid type '%s'. Expected a string or Buffer.", 500, TypeError)
|
47 | createError('FST_ERR_REP_ALREADY_SENT', 'Reply was already sent.')
|
48 | createError('FST_ERR_REP_SENT_VALUE', 'The only possible value for reply.sent is true.')
|
49 | createError('FST_ERR_SEND_INSIDE_ONERR', 'You cannot use `send` inside the `onError` hook')
|
50 | createError('FST_ERR_SEND_UNDEFINED_ERR', 'Undefined error has occured')
|
51 | createError('FST_ERR_BAD_STATUS_CODE', 'Called reply with an invalid status code: %s')
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | createError('FST_ERR_SCH_MISSING_ID', 'Missing schema $id property')
|
57 | createError('FST_ERR_SCH_ALREADY_PRESENT', "Schema with id '%s' already declared!")
|
58 | createError('FST_ERR_SCH_NOT_PRESENT', "Schema with id '%s' does not exist!")
|
59 | createError('FST_ERR_SCH_DUPLICATE', "Schema with '%s' already present!")
|
60 | createError('FST_ERR_SCH_BUILD', 'Failed building the schema for %s: %s, due error %s')
|
61 | createError('FST_ERR_SCH_MISSING_COMPILER', 'You must provide a schemaCompiler to route %s %s to use the schemaResolver')
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | createError('FST_ERR_PROMISE_NOT_FULLFILLED', "Promise may not be fulfilled with 'undefined' when statusCode is not 204")
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | createError('FST_ERR_HTTP2_INVALID_VERSION', 'HTTP2 is available only from node >= 8.8.1')
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | createError('FST_ERR_INIT_OPTS_INVALID', "Invalid initialization options: '%s'")
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | createError('FST_ERR_BAD_URL', "'%s' is not a valid url component", 400)
|
82 |
|
83 | function createError (code, message, statusCode = 500, Base = Error) {
|
84 | if (!code) throw new Error('Fastify error code must not be empty')
|
85 | if (!message) throw new Error('Fastify error message must not be empty')
|
86 |
|
87 | code = code.toUpperCase()
|
88 |
|
89 | function FastifyError (a, b, c) {
|
90 | Error.captureStackTrace(this, FastifyError)
|
91 | this.name = `FastifyError [${code}]`
|
92 | this.code = code
|
93 |
|
94 |
|
95 | if (a && b && c) {
|
96 | this.message = format(message, a, b, c)
|
97 | } else if (a && b) {
|
98 | this.message = format(message, a, b)
|
99 | } else if (a) {
|
100 | this.message = format(message, a)
|
101 | } else {
|
102 | this.message = message
|
103 | }
|
104 |
|
105 | this.message = `${this.code}: ${this.message}`
|
106 | this.statusCode = statusCode || undefined
|
107 | }
|
108 | FastifyError.prototype[Symbol.toStringTag] = 'Error'
|
109 |
|
110 | inherits(FastifyError, Base)
|
111 |
|
112 | codes[code] = FastifyError
|
113 |
|
114 | return codes[code]
|
115 | }
|
116 |
|
117 | module.exports = { codes, createError }
|