fastify 4.5.1 → 4.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.5.1'
3
+ const VERSION = '4.5.2'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
package/lib/fourOhFour.js CHANGED
@@ -10,7 +10,8 @@ const {
10
10
  kCanSetNotFoundHandler,
11
11
  kFourOhFourLevelInstance,
12
12
  kFourOhFourContext,
13
- kHooks
13
+ kHooks,
14
+ kErrorHandler
14
15
  } = require('./symbols.js')
15
16
  const { lifecycleHooks } = require('./hooks')
16
17
  const { buildErrorHandler } = require('./error-handler.js')
@@ -148,6 +149,7 @@ function fourOhFour (options) {
148
149
  .map(h => h.bind(this))
149
150
  context[hook] = toSet.length ? toSet : null
150
151
  }
152
+ context.errorHandler = opts.errorHandler ? buildErrorHandler(this[kErrorHandler], opts.errorHandler) : this[kErrorHandler]
151
153
  })
152
154
 
153
155
  if (this[kFourOhFourContext] !== null && prefix === '/') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "4.5.1",
3
+ "version": "4.5.2",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
package/test/404s.test.js CHANGED
@@ -1944,3 +1944,68 @@ test('Send 404 when frameworkError calls reply.callNotFound', t => {
1944
1944
 
1945
1945
  t.end()
1946
1946
  })
1947
+
1948
+ test('hooks are applied to not found handlers', async ({ equal }) => {
1949
+ const fastify = Fastify()
1950
+
1951
+ // adding await here is fundamental for this test
1952
+ await fastify.register(async function (fastify) {
1953
+ })
1954
+
1955
+ fastify.setErrorHandler(function (_, request, reply) {
1956
+ return reply.code(401).send({ error: 'Unauthorized' })
1957
+ })
1958
+
1959
+ fastify.addHook('preValidation', async function (request, reply) {
1960
+ throw new Error('kaboom')
1961
+ })
1962
+
1963
+ const { statusCode } = await fastify.inject('/')
1964
+ equal(statusCode, 401)
1965
+ })
1966
+
1967
+ test('hooks are applied to not found handlers /2', async ({ equal }) => {
1968
+ const fastify = Fastify()
1969
+
1970
+ async function plugin (fastify) {
1971
+ fastify.setErrorHandler(function (_, request, reply) {
1972
+ return reply.code(401).send({ error: 'Unauthorized' })
1973
+ })
1974
+ }
1975
+
1976
+ plugin[Symbol.for('skip-override')] = true
1977
+
1978
+ fastify.register(plugin)
1979
+
1980
+ fastify.addHook('preValidation', async function (request, reply) {
1981
+ throw new Error('kaboom')
1982
+ })
1983
+
1984
+ const { statusCode } = await fastify.inject('/')
1985
+ equal(statusCode, 401)
1986
+ })
1987
+
1988
+ test('hooks are applied to not found handlers /3', async ({ equal, fail }) => {
1989
+ const fastify = Fastify()
1990
+
1991
+ async function plugin (fastify) {
1992
+ fastify.setNotFoundHandler({ errorHandler }, async () => {
1993
+ fail('this should never be called')
1994
+ })
1995
+
1996
+ function errorHandler (_, request, reply) {
1997
+ return reply.code(401).send({ error: 'Unauthorized' })
1998
+ }
1999
+ }
2000
+
2001
+ plugin[Symbol.for('skip-override')] = true
2002
+
2003
+ fastify.register(plugin)
2004
+
2005
+ fastify.addHook('preValidation', async function (request, reply) {
2006
+ throw new Error('kaboom')
2007
+ })
2008
+
2009
+ const { statusCode } = await fastify.inject('/')
2010
+ equal(statusCode, 401)
2011
+ })