Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure preSerialization hooks are null by default #4203

Merged
merged 1 commit into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/context.js
Expand Up @@ -44,6 +44,7 @@ function Context ({
this.onTimeout = null
this.preHandler = null
this.onResponse = null
this.preSerialization = null
this.config = config
this.errorHandler = errorHandler || server[kErrorHandler]
this._middie = null
Expand Down
43 changes: 43 additions & 0 deletions test/hooks-async.test.js
Expand Up @@ -710,3 +710,46 @@ test('preSerializationEnd should handle errors if the serialize method throws',

t.end()
})

t.test('nested hooks to do not crash on 404', t => {
t.plan(2)
const fastify = Fastify()

fastify.get('/hello', (req, reply) => {
reply.send({ hello: 'world' })
})

fastify.register(async function (fastify) {
fastify.get('/something', (req, reply) => {
reply.callNotFound()
})

fastify.setNotFoundHandler(async (request, reply) => {
reply.statusCode = 404
return { status: 'nested-not-found' }
})

fastify.setErrorHandler(async (error, request, reply) => {
reply.statusCode = 500
return { status: 'nested-error', error }
})
}, { prefix: '/nested' })

fastify.setNotFoundHandler(async (request, reply) => {
reply.statusCode = 404
return { status: 'not-found' }
})

fastify.setErrorHandler(async (error, request, reply) => {
reply.statusCode = 500
return { status: 'error', error }
})

fastify.inject({
method: 'GET',
url: '/nested/something'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
})
})