Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed May 7, 2024
2 parents 9a6cf27 + fe25981 commit b5891ef
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/error-handler.js
Expand Up @@ -65,13 +65,17 @@ function handleError (reply, error, cb) {
return
}

const result = func(error, reply.request, reply)
if (result !== undefined) {
if (result !== null && typeof result.then === 'function') {
wrapThenable(result, reply)
} else {
reply.send(result)
try {
const result = func(error, reply.request, reply)
if (result !== undefined) {
if (result !== null && typeof result.then === 'function') {
wrapThenable(result, reply)
} else {
reply.send(result)
}
}
} catch (err) {
reply.send(err)
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/wrapThenable.js
Expand Up @@ -39,6 +39,8 @@ function wrapThenable (thenable, reply) {
// try-catch allow to re-throw error in error handler for async handler
try {
reply.send(err)
// The following should not happen
/* c8 ignore next 3 */
} catch (err) {
reply.send(err)
}
Expand Down
32 changes: 32 additions & 0 deletions test/500s.test.js
Expand Up @@ -188,3 +188,35 @@ test('cannot set childLoggerFactory after binding', t => {
}
})
})

test('catch synchronous errors', t => {
t.plan(3)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

fastify.setErrorHandler((_, req, reply) => {
throw new Error('kaboom2')
})

fastify.post('/', function (req, reply) {
reply.send(new Error('kaboom'))
})

fastify.inject({
method: 'POST',
url: '/',
headers: {
'Content-Type': 'application/json'
},
payload: JSON.stringify({ hello: 'world' }).substring(0, 5)
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 500)
t.same(res.json(), {
error: 'Internal Server Error',
message: 'kaboom2',
statusCode: 500
})
})
})
4 changes: 2 additions & 2 deletions test/types/request.test-d.ts
Expand Up @@ -81,8 +81,8 @@ const getHandler: RouteHandler = function (request, _reply) {
expectType<FastifyRequestContext<ContextConfigDefault>['config']>(request.routeConfig)
expectType<FastifyRequestContext<ContextConfigDefault>['config']>(request.routeOptions.config)
expectType<ContextConfigDefault & FastifyRouteConfig & FastifyContextConfig>(request.routeOptions.config)
expectType<FastifySchema>(request.routeSchema)
expectType<FastifySchema>(request.routeOptions.schema)
expectType<FastifySchema | undefined>(request.routeSchema)
expectType<FastifySchema | undefined>(request.routeOptions.schema)
expectType<RouteHandlerMethod>(request.routeOptions.handler)
expectType<string | undefined>(request.routeOptions.url)

Expand Down
4 changes: 2 additions & 2 deletions types/request.d.ts
Expand Up @@ -31,7 +31,7 @@ export interface RequestRouteOptions<ContextConfig = ContextConfigDefault, Schem
exposeHeadRoute: boolean;
prefixTrailingSlash: string;
config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
schema: SchemaCompiler;
schema?: SchemaCompiler; // it is empty for 404 requests
handler: RouteHandlerMethod;
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
body: RequestType['body'];
context: FastifyRequestContext<ContextConfig>;
routeConfig: FastifyRequestContext<ContextConfig>['config'];
routeSchema: FastifySchema
routeSchema?: FastifySchema; // it is empty for 404 requests

/** in order for this to be used the user should ensure they have set the attachValidation option. */
validationError?: Error & { validation: any; validationContext: string };
Expand Down

0 comments on commit b5891ef

Please sign in to comment.