Skip to content

Commit d88c1ab

Browse files
committed
chore: Updated dependencies.
1 parent 3348acf commit d88c1ab

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,24 @@
4141
"postpublish": "git push origin && git push origin -f --tags"
4242
},
4343
"dependencies": {
44-
"ajv": "^8.10.0",
44+
"ajv": "^8.11.0",
4545
"fastify-plugin": "^3.0.1",
4646
"http-errors-enhanced": "^1.0.6"
4747
},
4848
"devDependencies": {
49-
"@cowtech/eslint-config": "^8.6.1",
50-
"@swc/cli": "^0.1.55",
51-
"@swc/core": "^1.2.150",
52-
"@types/node": "^17.0.21",
53-
"@types/tap": "^15.0.6",
49+
"@cowtech/eslint-config": "^8.7.0",
50+
"@swc/cli": "^0.1.57",
51+
"@swc/core": "^1.2.218",
52+
"@types/node": "^18.0.6",
53+
"@types/tap": "^15.0.7",
5454
"ajv-formats": "^2.1.1",
55-
"c8": "^7.11.0",
55+
"c8": "^7.11.3",
5656
"chokidar": "^3.5.3",
5757
"fastify": "^3.27.2",
58-
"prettier": "^2.5.1",
58+
"prettier": "^2.7.1",
5959
"tap": "^16.0.0",
60-
"ts-node": "^10.7.0",
61-
"typescript": "^4.6.2"
60+
"ts-node": "^10.9.1",
61+
"typescript": "^4.7.4"
6262
},
6363
"engines": {
6464
"node": ">=14.15.0"

test/hooks.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable @typescript-eslint/no-floating-promises */
2+
3+
import fastify, { FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'
4+
import { INTERNAL_SERVER_ERROR } from 'http-errors-enhanced'
5+
import t from 'tap'
6+
import { plugin as fastifyHttpErrorsEnhanced } from '../src/index.js'
7+
8+
function buildServer(options: FastifyPluginOptions = {}): FastifyInstance {
9+
const server = fastify({
10+
ajv: {
11+
customOptions: {
12+
removeAdditional: false,
13+
useDefaults: true,
14+
coerceTypes: true,
15+
allErrors: true
16+
}
17+
}
18+
})
19+
20+
server.register(fastifyHttpErrorsEnhanced, options)
21+
22+
server.get('/error', {
23+
handler() {
24+
const error = new Error('This was a generic message.')
25+
Object.assign(error, { id: 1, headers: { 'X-Custom-Header': 'Custom-Value' } })
26+
27+
return Promise.reject(error)
28+
}
29+
})
30+
31+
return server
32+
}
33+
34+
t.test('should correctly allow preprocessing of error before executing the handler', async t => {
35+
const server = buildServer({
36+
preHandler(error: FastifyError | Error) {
37+
Object.defineProperty(error, 'preHandlerExecuted', { enumerable: true, value: true })
38+
return error
39+
}
40+
})
41+
42+
const response = await server.inject({ method: 'GET', url: '/error' })
43+
44+
t.equal(response.statusCode, INTERNAL_SERVER_ERROR)
45+
t.equal(response.headers['x-custom-header'], 'Custom-Value')
46+
47+
const payload = JSON.parse(response.payload)
48+
t.match(payload.stack[0], /^Object\.handler \((?:file:\/\/)?\$ROOT\/test\/hooks\.test\.ts:\d+:\d+\)$/)
49+
delete payload.stack
50+
51+
t.same(payload, {
52+
error: 'Internal Server Error',
53+
message: '[Error] This was a generic message.',
54+
statusCode: INTERNAL_SERVER_ERROR,
55+
id: 1,
56+
preHandlerExecuted: true
57+
})
58+
})

0 commit comments

Comments
 (0)