Skip to content

Commit

Permalink
Fix 4204 (#4205)
Browse files Browse the repository at this point in the history
* set the errorHandler of the root 404 handler before starting

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* Update test/404s.test.js

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
mcollina and Uzlopak committed Aug 18, 2022
1 parent 208d05b commit 3fd294d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/fourOhFour.js
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
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 /1', async ({ equal }) => {
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)
})

0 comments on commit 3fd294d

Please sign in to comment.