Skip to content

Commit d74b2b4

Browse files
feat: include origin error message into serialization error (fastify#4601)
1 parent b8ae90a commit d74b2b4

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

lib/error-handler.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const {
1111
} = require('./symbols.js')
1212

1313
const {
14-
FST_ERR_REP_INVALID_PAYLOAD_TYPE
14+
FST_ERR_REP_INVALID_PAYLOAD_TYPE,
15+
FST_ERR_FAILED_ERROR_SERIALIZATION
1516
} = require('./errors')
1617

1718
const { getSchemaSerializer } = require('./schemas')
@@ -113,11 +114,7 @@ function fallbackErrorHandler (error, reply, cb) {
113114
// error is always FST_ERR_SCH_SERIALIZATION_BUILD because this is called from route/compileSchemasForSerialization
114115
reply.log.error({ err, statusCode: res.statusCode }, 'The serializer for the given status code failed')
115116
reply.code(500)
116-
payload = serializeError({
117-
error: statusCodes['500'],
118-
message: err.message,
119-
statusCode: 500
120-
})
117+
payload = serializeError(new FST_ERR_FAILED_ERROR_SERIALIZATION(err.message, error.message))
121118
}
122119

123120
if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {

lib/errors.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ const codes = {
214214
'FST_ERR_BAD_TRAILER_VALUE',
215215
"Called reply.trailer('%s', fn) with an invalid type: %s. Expected a function."
216216
),
217+
FST_ERR_FAILED_ERROR_SERIALIZATION: createError(
218+
'FST_ERR_FAILED_ERROR_SERIALIZATION',
219+
'Failed to serialize an error. Error: %s. Original error: %s'
220+
),
217221
FST_ERR_MISSING_SERIALIZATION_FN: createError(
218222
'FST_ERR_MISSING_SERIALIZATION_FN',
219223
'Missing serialization function. Key "%s"'

test/schema-serialization.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,9 @@ test('do not crash if status code serializer errors', async t => {
833833
t.equal(res.statusCode, 500)
834834
t.same(res.json(), {
835835
statusCode: 500,
836-
error: 'Internal Server Error',
837-
message: '"code" is required!'
836+
code: 'FST_ERR_FAILED_ERROR_SERIALIZATION',
837+
message: 'Failed to serialize an error. Error: "code" is required!. ' +
838+
'Original error: querystring must have required property \'foo\''
838839
})
839840
})
840841

types/errors.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type FastifyErrorCodes = Record<
2828
'FST_ERR_BAD_STATUS_CODE'|
2929
'FST_ERR_BAD_TRAILER_NAME' |
3030
'FST_ERR_BAD_TRAILER_VALUE' |
31+
'FST_ERR_FAILED_ERROR_SERIALIZATION' |
3132
'FST_ERR_MISSING_SERIALIZATION_FN' |
3233
'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION' |
3334
'FST_ERR_SCH_MISSING_ID' |

0 commit comments

Comments
 (0)