Skip to content

Commit eeec3d9

Browse files
committed
fix types and update handler definition
1 parent 2316f0e commit eeec3d9

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

packages/node/src/integrations/tracing/fastify.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1818
*
1919
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
2020
*/
21-
interface MinimalFastifyRequest {
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
interface MinimalFastifyRequest extends Record<string, any> {
2223
method?: string;
2324
2425
routeOptions?: {
@@ -32,14 +33,19 @@ interface MinimalFastifyRequest {
3233
*
3334
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts
3435
*/
35-
interface MinimalFastifyReply {
36+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37+
interface MinimalFastifyReply extends Record<string, any> {
3638
statusCode: number;
3739
}
3840

3941
// We inline the types we care about here
4042
interface Fastify {
4143
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4244
register: (plugin: any) => void;
45+
addHook: (hook: string, handler: (...params: unknown[]) => void) => void;
46+
}
47+
48+
interface FastifyWithHooks extends Omit<Fastify, 'addHook'> {
4349
addHook(
4450
hook: 'onError',
4551
handler: (request: MinimalFastifyRequest, reply: MinimalFastifyReply, error: Error) => void,
@@ -52,8 +58,8 @@ interface FastifyHandlerOptions {
5258
* Callback method deciding whether error should be captured and sent to Sentry
5359
*
5460
* @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)
5763
*
5864
* @example
5965
*
@@ -65,24 +71,21 @@ interface FastifyHandlerOptions {
6571
* });
6672
* ```
6773
*
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.
6975
*
7076
* ```typescript
7177
* import type { FastifyRequest, FastifyReply } from 'fastify';
7278
*
7379
* 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;
7583
* return reply.statusCode >= 500;
7684
* },
7785
* });
7886
* ```
7987
*/
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;
8689
}
8790

8891
const INTEGRATION_NAME = 'Fastify';
@@ -127,11 +130,13 @@ export const fastifyIntegration = defineIntegration(_fastifyIntegration);
127130

128131
/**
129132
* 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.
131135
*/
132136
function defaultShouldHandleError(_error: Error, _request: MinimalFastifyRequest, reply: MinimalFastifyReply): boolean {
133137
const statusCode = reply.statusCode;
134-
return statusCode >= 500;
138+
// 3xx and 4xx errors are not sent by default.
139+
return statusCode >= 500 || statusCode <= 299;
135140
}
136141

137142
/**
@@ -158,7 +163,7 @@ export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<Fas
158163
const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError;
159164

160165
const plugin = Object.assign(
161-
function (fastify: Fastify, _options: unknown, done: () => void): void {
166+
function (fastify: FastifyWithHooks, _options: unknown, done: () => void): void {
162167
fastify.addHook('onError', async (request, reply, error) => {
163168
if (shouldHandleError(error, request, reply)) {
164169
captureException(error);
@@ -197,7 +202,6 @@ export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<Fas
197202
});
198203
}
199204

200-
// eslint-disable-next-line @typescript-eslint/unbound-method
201205
ensureIsWrapped(fastify.addHook, 'fastify');
202206
}
203207

0 commit comments

Comments
 (0)