-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(tanstackstart-react): Add global sentry exception middlewares #19330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+214
−17
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cd3f74e
feat(tanstackstart-react): Add sentry exception middleware
nicohrubec 71eed6f
yarn fix
nicohrubec f77a6f5
refactor
nicohrubec 9c6fa18
success
nicohrubec bbcd83e
clean tests
nicohrubec c4e49e3
.
nicohrubec 68ce37d
abc
nicohrubec 6ead206
no ssr exceptions captured
nicohrubec 554f305
.
nicohrubec d80c445
types seem to be fine now?
nicohrubec 1afee99
fix lint
nicohrubec ab0c88c
.
nicohrubec 9822576
.
nicohrubec aa06362
add comment and update mechanism
nicohrubec 398f1d8
fix lint
nicohrubec 271a788
yarn fix
nicohrubec 1d8b310
.
nicohrubec a3e26da
stale
nicohrubec 3061ef9
Add changelog
nicohrubec 77d0188
Merge branch 'develop' into nh/tss-error-middleware
nicohrubec ec4cffa
Merge branch 'develop' into nh/tss-error-middleware
nicohrubec d4ca904
.
nicohrubec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
8 changes: 1 addition & 7 deletions
8
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/routes/api.error.ts
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/routes/api.flush.ts
This file contains hidden or 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,13 @@ | ||
| import { createFileRoute } from '@tanstack/react-router'; | ||
| import { flush } from '@sentry/tanstackstart-react'; | ||
|
|
||
| export const Route = createFileRoute('/api/flush')({ | ||
| server: { | ||
| handlers: { | ||
| GET: async () => { | ||
| await flush(); | ||
| return new Response('ok'); | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/routes/ssr-error.tsx
This file contains hidden or 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,8 @@ | ||
| import { createFileRoute } from '@tanstack/react-router'; | ||
|
|
||
| export const Route = createFileRoute('/ssr-error')({ | ||
| loader: () => { | ||
| throw new Error('Sentry SSR Test Error'); | ||
| }, | ||
| component: () => <div>SSR Error Page</div>, | ||
| }); |
7 changes: 4 additions & 3 deletions
7
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/start.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { sentryGlobalFunctionMiddleware, sentryGlobalRequestMiddleware } from '@sentry/tanstackstart-react'; | ||
| import { createStart } from '@tanstack/react-start'; | ||
| // NOTE: These are NOT wrapped - auto-instrumentation via the Vite plugin will wrap them | ||
| import { globalRequestMiddleware, globalFunctionMiddleware } from './middleware'; | ||
| import { globalFunctionMiddleware, globalRequestMiddleware } from './middleware'; | ||
|
|
||
| export const startInstance = createStart(() => { | ||
| return { | ||
| requestMiddleware: [globalRequestMiddleware], | ||
| functionMiddleware: [globalFunctionMiddleware], | ||
| requestMiddleware: [sentryGlobalRequestMiddleware, globalRequestMiddleware], | ||
| functionMiddleware: [sentryGlobalFunctionMiddleware, globalFunctionMiddleware], | ||
| }; | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| export type TanStackMiddlewareBase = { | ||
| options?: { server?: (...args: unknown[]) => unknown }; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| '~types': any; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| options: { server?: (...args: any[]) => any }; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typescript kept complaining in my sample app that the types of the new sentry middlewares clash with what is expected from tanstack (it was still running fine thought), had to change it to this to fix that
nicohrubec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export type MiddlewareWrapperOptions = { | ||
|
|
||
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
packages/tanstackstart-react/src/server/globalMiddleware.ts
This file contains hidden or 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,46 @@ | ||
| import { addNonEnumerableProperty, captureException } from '@sentry/core'; | ||
| import type { TanStackMiddlewareBase } from '../common/types'; | ||
| import { SENTRY_INTERNAL } from './middleware'; | ||
|
|
||
| function createSentryMiddlewareHandler(mechanismType: string) { | ||
| return async function sentryMiddlewareHandler({ next }: { next: () => Promise<unknown> }): Promise<unknown> { | ||
| try { | ||
| return await next(); | ||
| } catch (e) { | ||
| captureException(e, { | ||
| mechanism: { type: mechanismType, handled: false }, | ||
| }); | ||
| throw e; | ||
| } | ||
| }; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Global request middleware that captures errors from API route requests. | ||
| * Should be added as the first entry in the `requestMiddleware` array of `createStart()`. | ||
| */ | ||
| export const sentryGlobalRequestMiddleware: TanStackMiddlewareBase = { | ||
| '~types': undefined, | ||
|
|
||
| options: { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| server: createSentryMiddlewareHandler('auto.middleware.tanstackstart.request') as (...args: any[]) => any, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Global function middleware that captures errors from server function invocations. | ||
| * Should be added as the first entry in the `functionMiddleware` array of `createStart()`. | ||
| */ | ||
| export const sentryGlobalFunctionMiddleware: TanStackMiddlewareBase = { | ||
| '~types': undefined, | ||
|
|
||
| options: { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| server: createSentryMiddlewareHandler('auto.middleware.tanstackstart.server_function') as (...args: any[]) => any, | ||
| }, | ||
| }; | ||
|
|
||
| // Mark as internal so the Vite auto-instrumentation plugin skips these middleware | ||
| addNonEnumerableProperty(sentryGlobalRequestMiddleware, SENTRY_INTERNAL, true); | ||
| addNonEnumerableProperty(sentryGlobalFunctionMiddleware, SENTRY_INTERNAL, true); | ||
This file contains hidden or 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 hidden or 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
71 changes: 71 additions & 0 deletions
71
packages/tanstackstart-react/test/server/globalMiddleware.test.ts
This file contains hidden or 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,71 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const captureExceptionSpy = vi.fn(); | ||
|
|
||
| vi.mock('@sentry/core', async importOriginal => { | ||
| const original = await importOriginal(); | ||
| return { | ||
| ...original, | ||
| captureException: (...args: unknown[]) => captureExceptionSpy(...args), | ||
| }; | ||
| }); | ||
|
|
||
| // Import after mocks are set up | ||
| const { sentryGlobalRequestMiddleware, sentryGlobalFunctionMiddleware } = | ||
| await import('../../src/server/globalMiddleware'); | ||
|
|
||
| describe('sentryGlobalRequestMiddleware', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('captures error with correct mechanism when next() throws', async () => { | ||
| const error = new Error('test error'); | ||
| const next = vi.fn().mockRejectedValue(error); | ||
|
|
||
| const serverFn = sentryGlobalRequestMiddleware.options.server!; | ||
|
|
||
| await expect(serverFn({ next })).rejects.toThrow('test error'); | ||
|
|
||
| expect(captureExceptionSpy).toHaveBeenCalledWith(error, { | ||
| mechanism: { type: 'auto.middleware.tanstackstart.request', handled: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('does not capture error when next() succeeds', async () => { | ||
| const next = vi.fn().mockResolvedValue('success'); | ||
|
|
||
| const serverFn = sentryGlobalRequestMiddleware.options.server!; | ||
| const result = await serverFn({ next }); | ||
|
|
||
| expect(result).toBe('success'); | ||
| expect(captureExceptionSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('has __SENTRY_INTERNAL__ flag set', () => { | ||
| expect((sentryGlobalRequestMiddleware as unknown as Record<string, unknown>)['__SENTRY_INTERNAL__']).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sentryGlobalFunctionMiddleware', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('captures error with correct mechanism when next() throws', async () => { | ||
| const error = new Error('test error'); | ||
| const next = vi.fn().mockRejectedValue(error); | ||
|
|
||
| const serverFn = sentryGlobalFunctionMiddleware.options.server!; | ||
|
|
||
| await expect(serverFn({ next })).rejects.toThrow('test error'); | ||
|
|
||
| expect(captureExceptionSpy).toHaveBeenCalledWith(error, { | ||
| mechanism: { type: 'auto.middleware.tanstackstart.server_function', handled: false }, | ||
| }); | ||
| }); | ||
|
|
||
| it('has __SENTRY_INTERNAL__ flag set', () => { | ||
| expect((sentryGlobalFunctionMiddleware as unknown as Record<string, unknown>)['__SENTRY_INTERNAL__']).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.