-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sveltekit): Don't capture thrown
Redirect
s as exceptions (#7731)
As outlined in the [SvelteKit docs](https://kit.svelte.dev/docs/load#redirects), users can `throw redirect(300, 'route/to/redirect')` in `load` functions. We don't want to capture these as errors and send them to Sentry.
- Loading branch information
Showing
6 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Redirect } from '@sveltejs/kit'; | ||
|
||
/** | ||
* Determines if a thrown "error" is a Redirect object which SvelteKit users can throw to redirect to another route | ||
* see: https://kit.svelte.dev/docs/modules#sveltejs-kit-redirect | ||
* @param error the potential redirect error | ||
*/ | ||
export function isRedirect(error: unknown): error is Redirect { | ||
if (error == null || typeof error !== 'object') { | ||
return false; | ||
} | ||
const hasValidLocation = 'location' in error && typeof error.location === 'string'; | ||
const hasValidStatus = | ||
'status' in error && typeof error.status === 'number' && error.status >= 300 && error.status <= 308; | ||
return hasValidLocation && hasValidStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isRedirect } from '../../src/common/utils'; | ||
|
||
describe('isRedirect', () => { | ||
it.each([ | ||
{ location: '/users/id', status: 300 }, | ||
{ location: '/users/id', status: 304 }, | ||
{ location: '/users/id', status: 308 }, | ||
{ location: '', status: 308 }, | ||
])('returns `true` for valid Redirect objects', redirectObject => { | ||
expect(isRedirect(redirectObject)).toBe(true); | ||
}); | ||
|
||
it.each([ | ||
300, | ||
'redirect', | ||
{ location: { route: { id: 'users/id' } }, status: 300 }, | ||
{ status: 308 }, | ||
{ location: '/users/id' }, | ||
{ location: '/users/id', status: 201 }, | ||
{ location: '/users/id', status: 400 }, | ||
{ location: '/users/id', status: 500 }, | ||
])('returns `false` for invalid Redirect objects', redirectObject => { | ||
expect(isRedirect(redirectObject)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters