UNPKG

4.29 kBJavaScriptView Raw
1'use strict'
2
3const { inherits, format } = require('util')
4const codes = {}
5
6/**
7 * Basic
8 */
9
10createError('FST_ERR_NOT_FOUND', 'Not Found', 404)
11
12/**
13 * ContentTypeParser
14*/
15createError('FST_ERR_CTP_ALREADY_PRESENT', "Content type parser '%s' already present.")
16createError('FST_ERR_CTP_INVALID_TYPE', 'The content type should be a string', 500, TypeError)
17createError('FST_ERR_CTP_EMPTY_TYPE', 'The content type cannot be an empty string', 500, TypeError)
18createError('FST_ERR_CTP_INVALID_HANDLER', 'The content type handler should be a function', 500, TypeError)
19createError('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)
20createError('FST_ERR_CTP_BODY_TOO_LARGE', 'Request body is too large', 413, RangeError)
21createError('FST_ERR_CTP_INVALID_MEDIA_TYPE', 'Unsupported Media Type: %s', 415)
22createError('FST_ERR_CTP_INVALID_CONTENT_LENGTH', 'Request body size did not match Content-Length', 400, RangeError)
23createError('FST_ERR_CTP_EMPTY_JSON_BODY', "Body cannot be empty when content-type is set to 'application/json'", 400)
24
25/**
26 * decorate
27*/
28createError('FST_ERR_DEC_ALREADY_PRESENT', "The decorator '%s' has already been added!")
29createError('FST_ERR_DEC_MISSING_DEPENDENCY', "The decorator is missing dependency '%s'.")
30createError('FST_ERR_DEC_AFTER_START', "The decorator '%s' has been added after start!")
31
32/**
33 * hooks
34*/
35createError('FST_ERR_HOOK_INVALID_TYPE', 'The hook name must be a string', 500, TypeError)
36createError('FST_ERR_HOOK_INVALID_HANDLER', 'The hook callback must be a function', 500, TypeError)
37
38/**
39 * logger
40*/
41createError('FST_ERR_LOG_INVALID_DESTINATION', 'Cannot specify both logger.stream and logger.file options')
42
43/**
44 * reply
45*/
46createError('FST_ERR_REP_INVALID_PAYLOAD_TYPE', "Attempted to send payload of invalid type '%s'. Expected a string or Buffer.", 500, TypeError)
47createError('FST_ERR_REP_ALREADY_SENT', 'Reply was already sent.')
48createError('FST_ERR_REP_SENT_VALUE', 'The only possible value for reply.sent is true.')
49createError('FST_ERR_SEND_INSIDE_ONERR', 'You cannot use `send` inside the `onError` hook')
50createError('FST_ERR_SEND_UNDEFINED_ERR', 'Undefined error has occured')
51createError('FST_ERR_BAD_STATUS_CODE', 'Called reply with an invalid status code: %s')
52
53/**
54 * schemas
55*/
56createError('FST_ERR_SCH_MISSING_ID', 'Missing schema $id property')
57createError('FST_ERR_SCH_ALREADY_PRESENT', "Schema with id '%s' already declared!")
58createError('FST_ERR_SCH_NOT_PRESENT', "Schema with id '%s' does not exist!")
59createError('FST_ERR_SCH_DUPLICATE', "Schema with '%s' already present!")
60createError('FST_ERR_SCH_BUILD', 'Failed building the schema for %s: %s, due error %s')
61createError('FST_ERR_SCH_MISSING_COMPILER', 'You must provide a schemaCompiler to route %s %s to use the schemaResolver')
62
63/**
64 * wrapThenable
65 */
66createError('FST_ERR_PROMISE_NOT_FULLFILLED', "Promise may not be fulfilled with 'undefined' when statusCode is not 204")
67
68/**
69 * http2
70 */
71createError('FST_ERR_HTTP2_INVALID_VERSION', 'HTTP2 is available only from node >= 8.8.1')
72
73/**
74 * initialConfig
75 */
76createError('FST_ERR_INIT_OPTS_INVALID', "Invalid initialization options: '%s'")
77
78/**
79 * router
80 */
81createError('FST_ERR_BAD_URL', "'%s' is not a valid url component", 400)
82
83function 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 // more performant than spread (...) operator
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
117module.exports = { codes, createError }