UNPKG

13.4 kBJavaScriptView Raw
1'use strict'
2
3const createError = require('@fastify/error')
4
5const codes = {
6 /**
7 * Basic
8 */
9 FST_ERR_NOT_FOUND: createError(
10 'FST_ERR_NOT_FOUND',
11 'Not Found',
12 404
13 ),
14 FST_ERR_OPTIONS_NOT_OBJ: createError(
15 'FST_ERR_OPTIONS_NOT_OBJ',
16 'Options must be an object',
17 500,
18 TypeError
19 ),
20 FST_ERR_QSP_NOT_FN: createError(
21 'FST_ERR_QSP_NOT_FN',
22 "querystringParser option should be a function, instead got '%s'",
23 500,
24 TypeError
25 ),
26 FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN: createError(
27 'FST_ERR_SCHEMA_CONTROLLER_BUCKET_OPT_NOT_FN',
28 "schemaController.bucket option should be a function, instead got '%s'",
29 500,
30 TypeError
31 ),
32 FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN: createError(
33 'FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN',
34 "schemaErrorFormatter option should be a non async function. Instead got '%s'.",
35 500,
36 TypeError
37 ),
38 FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ: createError(
39 'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_OBJ',
40 "ajv.customOptions option should be an object, instead got '%s'",
41 500,
42 TypeError
43 ),
44 FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR: createError(
45 'FST_ERR_AJV_CUSTOM_OPTIONS_OPT_NOT_ARR',
46 "ajv.plugins option should be an array, instead got '%s'",
47 500,
48 TypeError
49 ),
50 FST_ERR_VERSION_CONSTRAINT_NOT_STR: createError(
51 'FST_ERR_VERSION_CONSTRAINT_NOT_STR',
52 'Version constraint should be a string.',
53 500,
54 TypeError
55 ),
56 FST_ERR_VALIDATION: createError(
57 'FST_ERR_VALIDATION',
58 '%s',
59 400
60 ),
61 FST_ERR_LISTEN_OPTIONS_INVALID: createError(
62 'FST_ERR_LISTEN_OPTIONS_INVALID',
63 "Invalid listen options: '%s'",
64 500,
65 TypeError
66 ),
67 FST_ERR_ERROR_HANDLER_NOT_FN: createError(
68 'FST_ERR_ERROR_HANDLER_NOT_FN',
69 'Error Handler must be a function',
70 500,
71 TypeError
72 ),
73
74 /**
75 * ContentTypeParser
76 */
77 FST_ERR_CTP_ALREADY_PRESENT: createError(
78 'FST_ERR_CTP_ALREADY_PRESENT',
79 "Content type parser '%s' already present."
80 ),
81 FST_ERR_CTP_INVALID_TYPE: createError(
82 'FST_ERR_CTP_INVALID_TYPE',
83 'The content type should be a string or a RegExp',
84 500,
85 TypeError
86 ),
87 FST_ERR_CTP_EMPTY_TYPE: createError(
88 'FST_ERR_CTP_EMPTY_TYPE',
89 'The content type cannot be an empty string',
90 500,
91 TypeError
92 ),
93 FST_ERR_CTP_INVALID_HANDLER: createError(
94 'FST_ERR_CTP_INVALID_HANDLER',
95 'The content type handler should be a function',
96 500,
97 TypeError
98 ),
99 FST_ERR_CTP_INVALID_PARSE_TYPE: createError(
100 'FST_ERR_CTP_INVALID_PARSE_TYPE',
101 "The body parser can only parse your data as 'string' or 'buffer', you asked '%s' which is not supported.",
102 500,
103 TypeError
104 ),
105 FST_ERR_CTP_BODY_TOO_LARGE: createError(
106 'FST_ERR_CTP_BODY_TOO_LARGE',
107 'Request body is too large',
108 413,
109 RangeError
110 ),
111 FST_ERR_CTP_INVALID_MEDIA_TYPE: createError(
112 'FST_ERR_CTP_INVALID_MEDIA_TYPE',
113 'Unsupported Media Type: %s',
114 415
115 ),
116 FST_ERR_CTP_INVALID_CONTENT_LENGTH: createError(
117 'FST_ERR_CTP_INVALID_CONTENT_LENGTH',
118 'Request body size did not match Content-Length',
119 400,
120 RangeError
121 ),
122 FST_ERR_CTP_EMPTY_JSON_BODY: createError(
123 'FST_ERR_CTP_EMPTY_JSON_BODY',
124 "Body cannot be empty when content-type is set to 'application/json'",
125 400
126 ),
127 FST_ERR_CTP_INSTANCE_ALREADY_STARTED: createError(
128 'FST_ERR_CTP_INSTANCE_ALREADY_STARTED',
129 'Cannot call "%s" when fastify instance is already started!',
130 400
131 ),
132
133 /**
134 * decorate
135 */
136 FST_ERR_DEC_ALREADY_PRESENT: createError(
137 'FST_ERR_DEC_ALREADY_PRESENT',
138 "The decorator '%s' has already been added!"
139 ),
140 FST_ERR_DEC_DEPENDENCY_INVALID_TYPE: createError(
141 'FST_ERR_DEC_DEPENDENCY_INVALID_TYPE',
142 "The dependencies of decorator '%s' must be of type Array.",
143 500,
144 TypeError
145 ),
146 FST_ERR_DEC_MISSING_DEPENDENCY: createError(
147 'FST_ERR_DEC_MISSING_DEPENDENCY',
148 "The decorator is missing dependency '%s'."
149 ),
150 FST_ERR_DEC_AFTER_START: createError(
151 'FST_ERR_DEC_AFTER_START',
152 "The decorator '%s' has been added after start!"
153 ),
154
155 /**
156 * hooks
157 */
158 FST_ERR_HOOK_INVALID_TYPE: createError(
159 'FST_ERR_HOOK_INVALID_TYPE',
160 'The hook name must be a string',
161 500,
162 TypeError
163 ),
164 FST_ERR_HOOK_INVALID_HANDLER: createError(
165 'FST_ERR_HOOK_INVALID_HANDLER',
166 '%s hook should be a function, instead got %s',
167 500,
168 TypeError
169 ),
170 FST_ERR_HOOK_INVALID_ASYNC_HANDLER: createError(
171 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER',
172 'Async function has too many arguments. Async hooks should not use the \'done\' argument.',
173 500,
174 TypeError
175 ),
176 FST_ERR_HOOK_NOT_SUPPORTED: createError(
177 'FST_ERR_HOOK_NOT_SUPPORTED',
178 '%s hook not supported!',
179 500,
180 TypeError
181 ),
182
183 /**
184 * Middlewares
185 */
186 FST_ERR_MISSING_MIDDLEWARE: createError(
187 'FST_ERR_MISSING_MIDDLEWARE',
188 'You must register a plugin for handling middlewares, visit fastify.dev/docs/latest/Reference/Middleware/ for more info.',
189 500
190 ),
191
192 FST_ERR_HOOK_TIMEOUT: createError(
193 'FST_ERR_HOOK_TIMEOUT',
194 "A callback for '%s' hook timed out. You may have forgotten to call 'done' function or to resolve a Promise"
195 ),
196
197 /**
198 * logger
199 */
200 FST_ERR_LOG_INVALID_DESTINATION: createError(
201 'FST_ERR_LOG_INVALID_DESTINATION',
202 'Cannot specify both logger.stream and logger.file options'
203 ),
204
205 FST_ERR_LOG_INVALID_LOGGER: createError(
206 'FST_ERR_LOG_INVALID_LOGGER',
207 "Invalid logger object provided. The logger instance should have these functions(s): '%s'.",
208 500,
209 TypeError
210 ),
211
212 /**
213 * reply
214 */
215 FST_ERR_REP_INVALID_PAYLOAD_TYPE: createError(
216 'FST_ERR_REP_INVALID_PAYLOAD_TYPE',
217 "Attempted to send payload of invalid type '%s'. Expected a string or Buffer.",
218 500,
219 TypeError
220 ),
221 FST_ERR_REP_RESPONSE_BODY_CONSUMED: createError(
222 'FST_ERR_REP_RESPONSE_BODY_CONSUMED',
223 'Response.body is already consumed.'
224 ),
225 FST_ERR_REP_ALREADY_SENT: createError(
226 'FST_ERR_REP_ALREADY_SENT',
227 'Reply was already sent, did you forget to "return reply" in "%s" (%s)?'
228 ),
229 FST_ERR_REP_SENT_VALUE: createError(
230 'FST_ERR_REP_SENT_VALUE',
231 'The only possible value for reply.sent is true.',
232 500,
233 TypeError
234 ),
235 FST_ERR_SEND_INSIDE_ONERR: createError(
236 'FST_ERR_SEND_INSIDE_ONERR',
237 'You cannot use `send` inside the `onError` hook'
238 ),
239 FST_ERR_SEND_UNDEFINED_ERR: createError(
240 'FST_ERR_SEND_UNDEFINED_ERR',
241 'Undefined error has occurred'
242 ),
243 FST_ERR_BAD_STATUS_CODE: createError(
244 'FST_ERR_BAD_STATUS_CODE',
245 'Called reply with an invalid status code: %s'
246 ),
247 FST_ERR_BAD_TRAILER_NAME: createError(
248 'FST_ERR_BAD_TRAILER_NAME',
249 'Called reply.trailer with an invalid header name: %s'
250 ),
251 FST_ERR_BAD_TRAILER_VALUE: createError(
252 'FST_ERR_BAD_TRAILER_VALUE',
253 "Called reply.trailer('%s', fn) with an invalid type: %s. Expected a function."
254 ),
255 FST_ERR_FAILED_ERROR_SERIALIZATION: createError(
256 'FST_ERR_FAILED_ERROR_SERIALIZATION',
257 'Failed to serialize an error. Error: %s. Original error: %s'
258 ),
259 FST_ERR_MISSING_SERIALIZATION_FN: createError(
260 'FST_ERR_MISSING_SERIALIZATION_FN',
261 'Missing serialization function. Key "%s"'
262 ),
263 FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN: createError(
264 'FST_ERR_MISSING_CONTENTTYPE_SERIALIZATION_FN',
265 'Missing serialization function. Key "%s:%s"'
266 ),
267 FST_ERR_REQ_INVALID_VALIDATION_INVOCATION: createError(
268 'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION',
269 'Invalid validation invocation. Missing validation function for HTTP part "%s" nor schema provided.'
270 ),
271
272 /**
273 * schemas
274 */
275 FST_ERR_SCH_MISSING_ID: createError(
276 'FST_ERR_SCH_MISSING_ID',
277 'Missing schema $id property'
278 ),
279 FST_ERR_SCH_ALREADY_PRESENT: createError(
280 'FST_ERR_SCH_ALREADY_PRESENT',
281 "Schema with id '%s' already declared!"
282 ),
283 FST_ERR_SCH_CONTENT_MISSING_SCHEMA: createError(
284 'FST_ERR_SCH_CONTENT_MISSING_SCHEMA',
285 "Schema is missing for the content type '%s'"
286 ),
287 FST_ERR_SCH_DUPLICATE: createError(
288 'FST_ERR_SCH_DUPLICATE',
289 "Schema with '%s' already present!"
290 ),
291 FST_ERR_SCH_VALIDATION_BUILD: createError(
292 'FST_ERR_SCH_VALIDATION_BUILD',
293 'Failed building the validation schema for %s: %s, due to error %s'
294 ),
295 FST_ERR_SCH_SERIALIZATION_BUILD: createError(
296 'FST_ERR_SCH_SERIALIZATION_BUILD',
297 'Failed building the serialization schema for %s: %s, due to error %s'
298 ),
299 FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX: createError(
300 'FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX',
301 'response schemas should be nested under a valid status code, e.g { 2xx: { type: "object" } }'
302 ),
303
304 /**
305 * http2
306 */
307 FST_ERR_HTTP2_INVALID_VERSION: createError(
308 'FST_ERR_HTTP2_INVALID_VERSION',
309 'HTTP2 is available only from node >= 8.8.1'
310 ),
311
312 /**
313 * initialConfig
314 */
315 FST_ERR_INIT_OPTS_INVALID: createError(
316 'FST_ERR_INIT_OPTS_INVALID',
317 "Invalid initialization options: '%s'"
318 ),
319 FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE: createError(
320 'FST_ERR_FORCE_CLOSE_CONNECTIONS_IDLE_NOT_AVAILABLE',
321 "Cannot set forceCloseConnections to 'idle' as your HTTP server does not support closeIdleConnections method"
322 ),
323
324 /**
325 * router
326 */
327 FST_ERR_DUPLICATED_ROUTE: createError(
328 'FST_ERR_DUPLICATED_ROUTE',
329 "Method '%s' already declared for route '%s'"
330 ),
331 FST_ERR_BAD_URL: createError(
332 'FST_ERR_BAD_URL',
333 "'%s' is not a valid url component",
334 400,
335 URIError
336 ),
337 FST_ERR_ASYNC_CONSTRAINT: createError(
338 'FST_ERR_ASYNC_CONSTRAINT',
339 'Unexpected error from async constraint',
340 500
341 ),
342 FST_ERR_DEFAULT_ROUTE_INVALID_TYPE: createError(
343 'FST_ERR_DEFAULT_ROUTE_INVALID_TYPE',
344 'The defaultRoute type should be a function',
345 500,
346 TypeError
347 ),
348 FST_ERR_INVALID_URL: createError(
349 'FST_ERR_INVALID_URL',
350 "URL must be a string. Received '%s'",
351 400,
352 TypeError
353 ),
354 FST_ERR_ROUTE_OPTIONS_NOT_OBJ: createError(
355 'FST_ERR_ROUTE_OPTIONS_NOT_OBJ',
356 'Options for "%s:%s" route must be an object',
357 500,
358 TypeError
359 ),
360 FST_ERR_ROUTE_DUPLICATED_HANDLER: createError(
361 'FST_ERR_ROUTE_DUPLICATED_HANDLER',
362 'Duplicate handler for "%s:%s" route is not allowed!',
363 500
364 ),
365 FST_ERR_ROUTE_HANDLER_NOT_FN: createError(
366 'FST_ERR_ROUTE_HANDLER_NOT_FN',
367 'Error Handler for %s:%s route, if defined, must be a function',
368 500,
369 TypeError
370 ),
371 FST_ERR_ROUTE_MISSING_HANDLER: createError(
372 'FST_ERR_ROUTE_MISSING_HANDLER',
373 'Missing handler function for "%s:%s" route.',
374 500
375 ),
376 FST_ERR_ROUTE_METHOD_INVALID: createError(
377 'FST_ERR_ROUTE_METHOD_INVALID',
378 'Provided method is invalid!',
379 500,
380 TypeError
381 ),
382 FST_ERR_ROUTE_METHOD_NOT_SUPPORTED: createError(
383 'FST_ERR_ROUTE_METHOD_NOT_SUPPORTED',
384 '%s method is not supported.',
385 500
386 ),
387 FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED: createError(
388 'FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED',
389 'Body validation schema for %s:%s route is not supported!',
390 500
391 ),
392 FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT: createError(
393 'FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT',
394 "'bodyLimit' option must be an integer > 0. Got '%s'",
395 500,
396 TypeError
397 ),
398 FST_ERR_ROUTE_REWRITE_NOT_STR: createError(
399 'FST_ERR_ROUTE_REWRITE_NOT_STR',
400 'Rewrite url for "%s" needs to be of type "string" but received "%s"',
401 500,
402 TypeError
403 ),
404
405 /**
406 * again listen when close server
407 */
408 FST_ERR_REOPENED_CLOSE_SERVER: createError(
409 'FST_ERR_REOPENED_CLOSE_SERVER',
410 'Fastify has already been closed and cannot be reopened'
411 ),
412 FST_ERR_REOPENED_SERVER: createError(
413 'FST_ERR_REOPENED_SERVER',
414 'Fastify is already listening'
415 ),
416 FST_ERR_INSTANCE_ALREADY_LISTENING: createError(
417 'FST_ERR_INSTANCE_ALREADY_LISTENING',
418 'Fastify instance is already listening. %s'
419 ),
420
421 /**
422 * plugin
423 */
424 FST_ERR_PLUGIN_VERSION_MISMATCH: createError(
425 'FST_ERR_PLUGIN_VERSION_MISMATCH',
426 "fastify-plugin: %s - expected '%s' fastify version, '%s' is installed"
427 ),
428 FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE: createError(
429 'FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE',
430 "The decorator '%s'%s is not present in %s"
431 ),
432
433 /**
434 * Avvio Errors
435 */
436 FST_ERR_PLUGIN_CALLBACK_NOT_FN: createError(
437 'FST_ERR_PLUGIN_CALLBACK_NOT_FN',
438 'fastify-plugin: %s',
439 500,
440 TypeError
441 ),
442 FST_ERR_PLUGIN_NOT_VALID: createError(
443 'FST_ERR_PLUGIN_NOT_VALID',
444 'fastify-plugin: %s'
445 ),
446 FST_ERR_ROOT_PLG_BOOTED: createError(
447 'FST_ERR_ROOT_PLG_BOOTED',
448 'fastify-plugin: %s'
449 ),
450 FST_ERR_PARENT_PLUGIN_BOOTED: createError(
451 'FST_ERR_PARENT_PLUGIN_BOOTED',
452 'fastify-plugin: %s'
453 ),
454 FST_ERR_PLUGIN_TIMEOUT: createError(
455 'FST_ERR_PLUGIN_TIMEOUT',
456 'fastify-plugin: %s'
457 )
458}
459
460function appendStackTrace (oldErr, newErr) {
461 newErr.cause = oldErr
462
463 return newErr
464}
465
466module.exports = codes
467module.exports.appendStackTrace = appendStackTrace
468module.exports.AVVIO_ERRORS_MAP = {
469 AVV_ERR_CALLBACK_NOT_FN: codes.FST_ERR_PLUGIN_CALLBACK_NOT_FN,
470 AVV_ERR_PLUGIN_NOT_VALID: codes.FST_ERR_PLUGIN_NOT_VALID,
471 AVV_ERR_ROOT_PLG_BOOTED: codes.FST_ERR_ROOT_PLG_BOOTED,
472 AVV_ERR_PARENT_PLG_LOADED: codes.FST_ERR_PARENT_PLUGIN_BOOTED,
473 AVV_ERR_READY_TIMEOUT: codes.FST_ERR_PLUGIN_TIMEOUT,
474 AVV_ERR_PLUGIN_EXEC_TIMEOUT: codes.FST_ERR_PLUGIN_TIMEOUT
475}