UNPKG

3.61 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'.`)
30
31/**
32 * hooks
33*/
34createError('FST_ERR_HOOK_INVALID_TYPE', `The hook name must be a string`, 500, TypeError)
35createError('FST_ERR_HOOK_INVALID_HANDLER', `The hook callback must be a function`, 500, TypeError)
36
37/**
38 * logger
39*/
40createError('FST_ERR_LOG_INVALID_DESTINATION', `Cannot specify both logger.stream and logger.file options`)
41
42/**
43 * reply
44*/
45createError('FST_ERR_REP_INVALID_PAYLOAD_TYPE', `Attempted to send payload of invalid type '%s'. Expected a string or Buffer.`, 500, TypeError)
46createError('FST_ERR_REP_ALREADY_SENT', 'Reply was already sent.')
47createError('FST_ERR_REP_SENT_VALUE', 'The only possible value for reply.sent is true.')
48createError('FST_ERR_SEND_INSIDE_ONERR', 'You cannot use `send` inside the `onError` hook')
49
50/**
51 * schemas
52*/
53createError('FST_ERR_SCH_MISSING_ID', `Missing schema $id property`)
54createError('FST_ERR_SCH_ALREADY_PRESENT', `Schema with id '%s' already declared!`)
55createError('FST_ERR_SCH_NOT_PRESENT', `Schema with id '%s' does not exist!`)
56
57/**
58 * wrapThenable
59 */
60createError('FST_ERR_PROMISE_NOT_FULLFILLED', `Promise may not be fulfilled with 'undefined' when statusCode is not 204`)
61
62/**
63 * http2
64 */
65createError('FST_ERR_HTTP2_INVALID_VERSION', `HTTP2 is available only from node >= 8.8.1`)
66
67/**
68 * initialConfig
69 */
70createError('FST_ERR_INIT_OPTS_INVALID', `Invalid initialization options: '%s'`)
71
72function createError (code, message, statusCode = 500, Base = Error) {
73 if (!code) throw new Error('Fastify error code must not be empty')
74 if (!message) throw new Error('Fastify error message must not be empty')
75
76 code = code.toUpperCase()
77
78 function FastifyError (a, b, c) {
79 Error.captureStackTrace(this, FastifyError)
80 this.name = `FastifyError [${code}]`
81 this.code = code
82
83 // more performant than spread (...) operator
84 if (a && b && c) {
85 this.message = format(message, a, b, c)
86 } else if (a && b) {
87 this.message = format(message, a, b)
88 } else if (a) {
89 this.message = format(message, a)
90 } else {
91 this.message = message
92 }
93
94 this.message = `${this.code}: ${this.message}`
95 this.statusCode = statusCode || undefined
96 }
97
98 inherits(FastifyError, Base)
99
100 codes[code] = FastifyError
101
102 return codes[code]
103}
104
105module.exports = { codes, createError }