Skip to content

Commit 54f8e3c

Browse files
authored
fix: cb is not a function at fallbackErrorHandler (fastify#5317)
* fix: cb is not a function at fallbackErrorHandler * Update docs/Reference/Errors.md Signed-off-by: Aras Abbasi <[email protected]> --------- Signed-off-by: Aras Abbasi <[email protected]>
1 parent 44ac4fb commit 54f8e3c

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

docs/Reference/Errors.md

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
- [FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE](#fst_err_plugin_not_present_in_instance)
9393
- [FST_ERR_VALIDATION](#fst_err_validation)
9494
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid)
95+
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn)
9596

9697
### Error Handling In Node.js
9798
<a id="error-handling"></a>
@@ -361,4 +362,5 @@ but the body is being consumed. | Make sure you don't consume the `Response.body
361362
| <a id="fst_err_plugin_not_present_in_instance">FST_ERR_PLUGIN_NOT_PRESENT_IN_INSTANCE</a> | The decorator is not present in the instance. | - | [#4554](https://github.com/fastify/fastify/pull/4554) |
362363
| <a id="fst_err_validation">FST_ERR_VALIDATION</a> | The Request failed the payload validation. | Check the request payload. | [#4824](https://github.com/fastify/fastify/pull/4824) |
363364
| <a id="fst_err_listen_options_invalid">FST_ERR_LISTEN_OPTIONS_INVALID</a> | Invalid listen options. | Check the listen options. | [#4886](https://github.com/fastify/fastify/pull/4886) |
365+
| <a id="fst_err_error_handler_not_fn">FST_ERR_ERROR_HANDLER_NOT_FN</a> | Error Handler must be a function | Provide a function to `setErrorHandler`. | [#5317](https://github.com/fastify/fastify/pull/5317) |
364366

fastify.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const {
7171
FST_ERR_INSTANCE_ALREADY_LISTENING,
7272
FST_ERR_REOPENED_CLOSE_SERVER,
7373
FST_ERR_ROUTE_REWRITE_NOT_STR,
74-
FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN
74+
FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN,
75+
FST_ERR_ERROR_HANDLER_NOT_FN
7576
} = errorCodes
7677

7778
const { buildErrorHandler } = require('./lib/error-handler.js')
@@ -844,6 +845,10 @@ function fastify (options) {
844845
function setErrorHandler (func) {
845846
throwIfAlreadyStarted('Cannot call "setErrorHandler"!')
846847

848+
if (typeof func !== 'function') {
849+
throw new FST_ERR_ERROR_HANDLER_NOT_FN()
850+
}
851+
847852
this[kErrorHandler] = buildErrorHandler(this[kErrorHandler], func.bind(this))
848853
return this
849854
}

lib/errors.js

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ const codes = {
6464
500,
6565
TypeError
6666
),
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+
),
6773

6874
/**
6975
* ContentTypeParser

test/internals/errors.test.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const errors = require('../../lib/errors')
55
const { readFileSync } = require('node:fs')
66
const { resolve } = require('node:path')
77

8-
test('should expose 79 errors', t => {
8+
test('should expose 80 errors', t => {
99
t.plan(1)
1010
const exportedKeys = Object.keys(errors)
1111
let counter = 0
@@ -14,11 +14,11 @@ test('should expose 79 errors', t => {
1414
counter++
1515
}
1616
}
17-
t.equal(counter, 79)
17+
t.equal(counter, 80)
1818
})
1919

2020
test('ensure name and codes of Errors are identical', t => {
21-
t.plan(79)
21+
t.plan(80)
2222
const exportedKeys = Object.keys(errors)
2323
for (const key of exportedKeys) {
2424
if (errors[key].name === 'FastifyError') {
@@ -827,8 +827,18 @@ test('FST_ERR_LISTEN_OPTIONS_INVALID', t => {
827827
t.ok(error instanceof TypeError)
828828
})
829829

830+
test('FST_ERR_ERROR_HANDLER_NOT_FN', t => {
831+
t.plan(5)
832+
const error = new errors.FST_ERR_ERROR_HANDLER_NOT_FN()
833+
t.equal(error.name, 'FastifyError')
834+
t.equal(error.code, 'FST_ERR_ERROR_HANDLER_NOT_FN')
835+
t.equal(error.message, 'Error Handler must be a function')
836+
t.equal(error.statusCode, 500)
837+
t.ok(error instanceof TypeError)
838+
})
839+
830840
test('Ensure that all errors are in Errors.md TOC', t => {
831-
t.plan(79)
841+
t.plan(80)
832842
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
833843

834844
const exportedKeys = Object.keys(errors)
@@ -840,7 +850,7 @@ test('Ensure that all errors are in Errors.md TOC', t => {
840850
})
841851

842852
test('Ensure that non-existing errors are not in Errors.md TOC', t => {
843-
t.plan(79)
853+
t.plan(80)
844854
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
845855

846856
const matchRE = / {4}- \[([A-Z0-9_]+)\]\(#[a-z0-9_]+\)/g
@@ -853,7 +863,7 @@ test('Ensure that non-existing errors are not in Errors.md TOC', t => {
853863
})
854864

855865
test('Ensure that all errors are in Errors.md documented', t => {
856-
t.plan(79)
866+
t.plan(80)
857867
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
858868

859869
const exportedKeys = Object.keys(errors)
@@ -865,7 +875,7 @@ test('Ensure that all errors are in Errors.md documented', t => {
865875
})
866876

867877
test('Ensure that non-existing errors are not in Errors.md documented', t => {
868-
t.plan(79)
878+
t.plan(80)
869879
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8')
870880

871881
const matchRE = /<a id="[0-9a-zA-Z_]+">([0-9a-zA-Z_]+)<\/a>/g

test/set-error-handler.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const Fastify = require('..')
6+
const { FST_ERR_ERROR_HANDLER_NOT_FN } = require('../lib/errors')
7+
8+
test('setErrorHandler should throw an error if the handler is not a function', t => {
9+
t.plan(1)
10+
const fastify = Fastify()
11+
12+
t.throws(() => fastify.setErrorHandler('not a function'), new FST_ERR_ERROR_HANDLER_NOT_FN())
13+
})

0 commit comments

Comments
 (0)