@@ -18,7 +18,8 @@ import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
18
18
*
19
19
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
20
20
*/
21
- interface MinimalFastifyRequest {
21
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
+ interface MinimalFastifyRequest extends Record < string , any > {
22
23
method ?: string ;
23
24
24
25
routeOptions ?: {
@@ -32,14 +33,19 @@ interface MinimalFastifyRequest {
32
33
*
33
34
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts
34
35
*/
35
- interface MinimalFastifyReply {
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ interface MinimalFastifyReply extends Record < string , any > {
36
38
statusCode : number ;
37
39
}
38
40
39
41
// We inline the types we care about here
40
42
interface Fastify {
41
43
// eslint-disable-next-line @typescript-eslint/no-explicit-any
42
44
register : ( plugin : any ) => void ;
45
+ addHook : ( hook : string , handler : ( ...params : unknown [ ] ) => void ) => void ;
46
+ }
47
+
48
+ interface FastifyWithHooks extends Omit < Fastify , 'addHook' > {
43
49
addHook (
44
50
hook : 'onError' ,
45
51
handler : ( request : MinimalFastifyRequest , reply : MinimalFastifyReply , error : Error ) => void ,
@@ -52,8 +58,8 @@ interface FastifyHandlerOptions {
52
58
* Callback method deciding whether error should be captured and sent to Sentry
53
59
*
54
60
* @param error Captured Fastify error
55
- * @param request Fastify request
56
- * @param reply Fastify reply
61
+ * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
62
+ * @param reply Fastify reply (or any object containing at least statusCode)
57
63
*
58
64
* @example
59
65
*
@@ -65,24 +71,21 @@ interface FastifyHandlerOptions {
65
71
* });
66
72
* ```
67
73
*
68
- * If using TypeScript, pass in the `FastifyRequest` and `FastifyReply` types to get type safety.
74
+ * If using TypeScript, you can cast the request and reply to get full type safety.
69
75
*
70
76
* ```typescript
71
77
* import type { FastifyRequest, FastifyReply } from 'fastify';
72
78
*
73
79
* setupFastifyErrorHandler(app, {
74
- * shouldHandleError<FastifyRequest, FastifyReply>(error, request, reply) {
80
+ * shouldHandleError(error, minimalRequest, minimalReply) {
81
+ * const request = minimalRequest as FastifyRequest;
82
+ * const reply = minimalReply as FastifyReply;
75
83
* return reply.statusCode >= 500;
76
84
* },
77
85
* });
78
86
* ```
79
87
*/
80
- shouldHandleError < FastifyRequest extends MinimalFastifyRequest , FastifyReply extends MinimalFastifyReply > (
81
- this : void ,
82
- error : Error ,
83
- request : FastifyRequest ,
84
- reply : FastifyReply ,
85
- ) : boolean ;
88
+ shouldHandleError : ( error : Error , request : MinimalFastifyRequest , reply : MinimalFastifyReply ) => boolean ;
86
89
}
87
90
88
91
const INTEGRATION_NAME = 'Fastify' ;
@@ -127,11 +130,13 @@ export const fastifyIntegration = defineIntegration(_fastifyIntegration);
127
130
128
131
/**
129
132
* Default function to determine if an error should be sent to Sentry
130
- * Only sends 5xx errors by default
133
+ *
134
+ * 3xx and 4xx errors are not sent by default.
131
135
*/
132
136
function defaultShouldHandleError ( _error : Error , _request : MinimalFastifyRequest , reply : MinimalFastifyReply ) : boolean {
133
137
const statusCode = reply . statusCode ;
134
- return statusCode >= 500 ;
138
+ // 3xx and 4xx errors are not sent by default.
139
+ return statusCode >= 500 || statusCode <= 299 ;
135
140
}
136
141
137
142
/**
@@ -158,7 +163,7 @@ export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<Fas
158
163
const shouldHandleError = options ?. shouldHandleError || defaultShouldHandleError ;
159
164
160
165
const plugin = Object . assign (
161
- function ( fastify : Fastify , _options : unknown , done : ( ) => void ) : void {
166
+ function ( fastify : FastifyWithHooks , _options : unknown , done : ( ) => void ) : void {
162
167
fastify . addHook ( 'onError' , async ( request , reply , error ) => {
163
168
if ( shouldHandleError ( error , request , reply ) ) {
164
169
captureException ( error ) ;
@@ -197,7 +202,6 @@ export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<Fas
197
202
} ) ;
198
203
}
199
204
200
- // eslint-disable-next-line @typescript-eslint/unbound-method
201
205
ensureIsWrapped ( fastify . addHook , 'fastify' ) ;
202
206
}
203
207
0 commit comments