@@ -2,6 +2,7 @@ import { addTracingExtensions, Hub, makeMain, Scope } from '@sentry/core';
22import { NodeClient } from '@sentry/node' ;
33import type { Transaction } from '@sentry/types' ;
44import type { Handle } from '@sveltejs/kit' ;
5+ import { redirect } from '@sveltejs/kit' ;
56import { vi } from 'vitest' ;
67
78import { sentryHandle , transformPageChunk } from '../../src/server/handle' ;
@@ -69,7 +70,21 @@ const enum Type {
6970 Async = 'async' ,
7071}
7172
72- function resolve ( type : Type , isError : boolean ) : Parameters < Handle > [ 0 ] [ 'resolve' ] {
73+ function resolve (
74+ type : Type ,
75+ isError : boolean ,
76+ throwSpecialError ?: 'redirect' | 'httpError4xx' | 'httpError5xx' ,
77+ ) : Parameters < Handle > [ 0 ] [ 'resolve' ] {
78+ if ( throwSpecialError === 'redirect' ) {
79+ throw redirect ( 302 , '/redirect' ) ;
80+ }
81+ if ( throwSpecialError === 'httpError4xx' ) {
82+ throw { status : 404 , body : 'Not found' } ;
83+ }
84+ if ( throwSpecialError === 'httpError5xx' ) {
85+ throw { status : 500 , body : 'Internal error' } ;
86+ }
87+
7388 if ( type === Type . Sync ) {
7489 return ( ..._args : unknown [ ] ) => {
7590 if ( isError ) {
@@ -292,6 +307,22 @@ describe('handleSentry', () => {
292307 }
293308 } ) ;
294309
310+ it ( "doesn't send redirects in a request handler to Sentry" , async ( ) => {
311+ try {
312+ await sentryHandle ( ) ( { event : mockEvent ( ) , resolve : resolve ( type , false , 'redirect' ) } ) ;
313+ } catch ( e ) {
314+ expect ( mockCaptureException ) . toBeCalledTimes ( 0 ) ;
315+ }
316+ } ) ;
317+
318+ it ( "doesn't send Http 4xx errors in a request handler to Sentry" , async ( ) => {
319+ try {
320+ await sentryHandle ( ) ( { event : mockEvent ( ) , resolve : resolve ( type , false , 'httpError4xx' ) } ) ;
321+ } catch ( e ) {
322+ expect ( mockCaptureException ) . toBeCalledTimes ( 0 ) ;
323+ }
324+ } ) ;
325+
295326 it ( 'calls `transformPageChunk`' , async ( ) => {
296327 const mockResolve = vi . fn ( ) . mockImplementation ( resolve ( type , isError ) ) ;
297328 const event = mockEvent ( ) ;
0 commit comments