Skip to content

Commit 1a779cb

Browse files
committed
feat: Allow skipping setNotFoundHandler.
1 parent f556a98 commit 1a779cb

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ npm install fastify-http-errors-enhanced --save
2121

2222
Register as a plugin, optional providing any of the following options:
2323

24+
- `handle404Errors`: If to set an handler via `setNotFoundHandler`.
2425
- `hideUnhandledErrors`: If to hide unhandled server errors or returning to the client including stack information. Default is to hide errors when `NODE_ENV` environment variable is `production`.
2526
- `convertValidationErrors`: Convert validation errors to a structured human readable object. Default is `true`.
2627
- `convertResponsesValidationErrors`: Convert response validation errors to a structured human readable object. Default is to enable when `NODE_ENV` environment variable is different from `production`.

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const plugin = fastifyPlugin(
1616
function (instance: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void {
1717
const isProduction = process.env.NODE_ENV === 'production'
1818
const convertResponsesValidationErrors = options.convertResponsesValidationErrors ?? !isProduction
19+
const handle404Errors = options.handle404Errors ?? true
1920

2021
const configuration: Configuration = {
2122
hideUnhandledErrors: options.hideUnhandledErrors ?? isProduction,
@@ -35,7 +36,10 @@ export const plugin = fastifyPlugin(
3536
})
3637

3738
instance.setErrorHandler(handleErrors)
38-
instance.setNotFoundHandler(handleNotFoundError)
39+
40+
if (handle404Errors) {
41+
instance.setNotFoundHandler(handleNotFoundError)
42+
}
3943

4044
if (convertResponsesValidationErrors) {
4145
instance.decorate(kHttpErrorsEnhancedResponseValidations, [])

test/index.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

3-
import fastify, { FastifyInstance, FastifyPluginOptions } from 'fastify'
3+
import fastify, { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify'
44
import {
55
BadGatewayError,
66
BAD_GATEWAY,
@@ -332,6 +332,40 @@ t.test('Plugin', t => {
332332
}
333333
)
334334

335+
t.test('should correctly install a not found error handler', async t => {
336+
const server = await buildServer()
337+
338+
const response = await server.inject({
339+
method: 'POST',
340+
url: '/not-found',
341+
headers: { 'content-type': 'image/png' }
342+
})
343+
344+
t.equal(response.statusCode, NOT_FOUND)
345+
t.same(JSON.parse(response.payload), {
346+
statusCode: 404,
347+
error: 'Not Found',
348+
message: 'Not found.'
349+
})
350+
})
351+
352+
t.test('should correctly skip installing a not found error handler', async t => {
353+
const server = await buildServer({ handle404Errors: false })
354+
355+
server.setNotFoundHandler((_: FastifyRequest, reply: FastifyReply) => {
356+
reply.code(444).send('NOT FOUND')
357+
})
358+
359+
const response = await server.inject({
360+
method: 'POST',
361+
url: '/not-found',
362+
headers: { 'content-type': 'image/png' }
363+
})
364+
365+
t.equal(response.statusCode, 444)
366+
t.same(response.payload, 'NOT FOUND')
367+
})
368+
335369
t.test('should correctly parse invalid content type errors', async t => {
336370
const server = await buildServer()
337371

0 commit comments

Comments
 (0)