Skip to content

Commit cee7f04

Browse files
committed
fix(cloudflare): Capture exceptions thrown in hono
1 parent 593d5b3 commit cee7f04

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/cloudflare/src/handler.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { init } from './sdk';
2626
* @param handler {ExportedHandler} The handler to wrap.
2727
* @returns The wrapped handler.
2828
*/
29+
// eslint-disable-next-line complexity
2930
export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostMetadata = unknown>(
3031
optionsCallback: (env: Env) => CloudflareOptions,
3132
handler: ExportedHandler<Env, QueueHandlerMessage, CfHostMetadata>,
@@ -47,6 +48,31 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
4748
markAsInstrumented(handler.fetch);
4849
}
4950

51+
/* hono does not reach the catch block of the fetch handler and captureException needs to be called in the hono errorHandler */
52+
if (
53+
'onError' in handler &&
54+
'errorHandler' in handler &&
55+
typeof handler.errorHandler === 'function' &&
56+
!isInstrumented(handler.errorHandler)
57+
) {
58+
handler.errorHandler = new Proxy(handler.errorHandler, {
59+
apply(target, thisArg, args) {
60+
const [err] = args;
61+
62+
captureException(err, {
63+
mechanism: {
64+
handled: false,
65+
type: 'cloudflare',
66+
},
67+
});
68+
69+
return Reflect.apply(target, thisArg, args);
70+
},
71+
});
72+
73+
markAsInstrumented(handler.errorHandler);
74+
}
75+
5076
if ('scheduled' in handler && typeof handler.scheduled === 'function' && !isInstrumented(handler.scheduled)) {
5177
handler.scheduled = new Proxy(handler.scheduled, {
5278
apply(target, thisArg, args: Parameters<ExportedHandlerScheduledHandler<Env>>) {

0 commit comments

Comments
 (0)