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

Fix 4204 #4205

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion lib/fourOhFour.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
kCanSetNotFoundHandler,
kFourOhFourLevelInstance,
kFourOhFourContext,
kHooks
kHooks,
kErrorHandler
} = require('./symbols.js')
const { lifecycleHooks } = require('./hooks')
const { buildErrorHandler } = require('./error-handler.js')
Expand Down Expand Up @@ -148,6 +149,7 @@ function fourOhFour (options) {
.map(h => h.bind(this))
context[hook] = toSet.length ? toSet : null
}
context.errorHandler = opts.errorHandler ? buildErrorHandler(this[kErrorHandler], opts.errorHandler) : this[kErrorHandler]
})

if (this[kFourOhFourContext] !== null && prefix === '/') {
Expand Down
65 changes: 65 additions & 0 deletions test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1944,3 +1944,68 @@ test('Send 404 when frameworkError calls reply.callNotFound', t => {

t.end()
})

test('hooks are applied to not found handlers', async ({ equal }) => {
mcollina marked this conversation as resolved.
Show resolved Hide resolved
const fastify = Fastify()

// adding await here is fundamental for this test
await fastify.register(async function (fastify) {
})

fastify.setErrorHandler(function (_, request, reply) {
return reply.code(401).send({ error: 'Unauthorized' })
})

fastify.addHook('preValidation', async function (request, reply) {
throw new Error('kaboom')
})

const { statusCode } = await fastify.inject('/')
equal(statusCode, 401)
})

test('hooks are applied to not found handlers /2', async ({ equal }) => {
const fastify = Fastify()

async function plugin (fastify) {
fastify.setErrorHandler(function (_, request, reply) {
return reply.code(401).send({ error: 'Unauthorized' })
})
}

plugin[Symbol.for('skip-override')] = true

fastify.register(plugin)

fastify.addHook('preValidation', async function (request, reply) {
throw new Error('kaboom')
})

const { statusCode } = await fastify.inject('/')
equal(statusCode, 401)
})

test('hooks are applied to not found handlers /3', async ({ equal, fail }) => {
const fastify = Fastify()

async function plugin (fastify) {
fastify.setNotFoundHandler({ errorHandler }, async () => {
fail('this should never be called')
})

function errorHandler (_, request, reply) {
return reply.code(401).send({ error: 'Unauthorized' })
}
}

plugin[Symbol.for('skip-override')] = true

fastify.register(plugin)

fastify.addHook('preValidation', async function (request, reply) {
throw new Error('kaboom')
})

const { statusCode } = await fastify.inject('/')
equal(statusCode, 401)
})