Skip to content

Commit 0ff3a72

Browse files
committed
feat: Output code property when it doesn't start with standard prefix.
1 parent 7dc7387 commit 0ff3a72

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The handler response format will compatible with standard fastify error response
3737
}
3838
```
3939

40+
If the original error's `code` properties does not start with `HTTP_ERROR_` ([http-errors-enhanced](https://github.com/ShogunPanda/http-errors-enhanced) standard error prefix), then the `code` is also included in output object.
4041
In addition, the response headers will contain all headers defined in `error.headers` and the response body will contain all additional enumerable properties of the error.
4142

4243
To clarify, take this server as a example:
@@ -49,7 +50,10 @@ server.register(require('fastify-http-errors-enhanced'))
4950

5051
server.get('/invalid', {
5152
handler: async function (request, reply) {
52-
throw new NotFoundError('You are not supposed to reach this.', { header: { 'X-Req-Id': request.id, id: 123 } })
53+
throw new NotFoundError('You are not supposed to reach this.', {
54+
header: { 'X-Req-Id': request.id, id: 123 },
55+
code: 'UNREACHABLE'
56+
})
5357
}
5458
})
5559

@@ -65,6 +69,7 @@ When hitting `/invalid` it will return the following:
6569
```json
6670
{
6771
"error": "Not Found",
72+
"code": "UNREACHABLE",
6873
"message": "You are not supposed to reach this.",
6974
"statusCode": 404,
7075
"id": 123

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dependencies": {
4040
"ajv": "^6.12.6",
4141
"fastify-plugin": "^3.0.0",
42-
"http-errors-enhanced": "^0.3.0"
42+
"http-errors-enhanced": "^0.4.2"
4343
},
4444
"devDependencies": {
4545
"@cowtech/eslint-config": "^7.10.1",

test/index.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ function routes(instance: FastifyInstance, _options: unknown, done: Callback): v
5353
}
5454
})
5555

56+
instance.get('/error-with-code', {
57+
async handler(): Promise<void> {
58+
const error = createError(BAD_GATEWAY, 'This was the error message.', { code: 'CODE' })
59+
60+
throw error
61+
}
62+
})
63+
5664
instance.get('/weird-code', {
5765
async handler(): Promise<void> {
5866
const error = new BadGatewayError('This was the error message.')
@@ -233,6 +241,20 @@ t.test('Plugin', (t: Test) => {
233241
})
234242
})
235243

244+
t.test('should correctly return error codes when not starting with the prefix', async (t: Test) => {
245+
await buildServer()
246+
247+
const response = await server!.inject({ method: 'GET', url: '/error-with-code' })
248+
249+
t.equal(response.statusCode, BAD_GATEWAY)
250+
t.deepEqual(JSON.parse(response.payload), {
251+
error: 'Bad Gateway',
252+
message: 'This was the error message.',
253+
statusCode: BAD_GATEWAY,
254+
code: 'CODE'
255+
})
256+
})
257+
236258
t.test('should correctly return server duck-typed errors', async (t: Test) => {
237259
await buildServer()
238260

@@ -569,7 +591,8 @@ t.test('Plugin', (t: Test) => {
569591
t.deepEqual(payload, {
570592
error: 'Internal Server Error',
571593
message: '[CODE] This was a generic message.',
572-
statusCode: INTERNAL_SERVER_ERROR
594+
statusCode: INTERNAL_SERVER_ERROR,
595+
code: 'CODE'
573596
})
574597
})
575598

@@ -581,7 +604,6 @@ t.test('Plugin', (t: Test) => {
581604
t.equal(response.statusCode, INTERNAL_SERVER_ERROR)
582605

583606
const payload = JSON.parse(response.payload)
584-
console.log(JSON.stringify(payload, null, 2))
585607
t.match(payload.stack[1], /wrapValidationError \(\$ROOT\/node_modules\/fastify\/.+:\d+:\d+\)/)
586608
delete payload.stack
587609

0 commit comments

Comments
 (0)