-
-
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.
feat(replay): Capture hydration error breadcrumb (#9759)
Adds a hydration error breadcrumb for nextjs / other react ssr frameworks. fixes #9649
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { ErrorEvent, Event } from '@sentry/types'; | ||
|
||
import type { ReplayContainer } from '../types'; | ||
import { createBreadcrumb } from '../util/createBreadcrumb'; | ||
import { isErrorEvent } from '../util/eventUtils'; | ||
import { addBreadcrumbEvent } from './util/addBreadcrumbEvent'; | ||
|
||
type BeforeSendEventCallback = (event: Event) => void; | ||
|
||
/** | ||
* Returns a listener to be added to `client.on('afterSendErrorEvent, listener)`. | ||
*/ | ||
export function handleBeforeSendEvent(replay: ReplayContainer): BeforeSendEventCallback { | ||
return (event: Event) => { | ||
if (!replay.isEnabled() || !isErrorEvent(event)) { | ||
return; | ||
} | ||
|
||
handleHydrationError(replay, event); | ||
}; | ||
} | ||
|
||
function handleHydrationError(replay: ReplayContainer, event: ErrorEvent): void { | ||
const exceptionValue = event.exception && event.exception.values && event.exception.values[0].value; | ||
if (typeof exceptionValue !== 'string') { | ||
return; | ||
} | ||
|
||
if ( | ||
// Only matches errors in production builds of react-dom | ||
// Example https://reactjs.org/docs/error-decoder.html?invariant=423 | ||
exceptionValue.match(/reactjs\.org\/docs\/error-decoder\.html\?invariant=(418|419|422|423|425)/) || | ||
// Development builds of react-dom | ||
// Example Text: content did not match. Server: "A" Client: "B" | ||
exceptionValue.match(/(hydration|content does not match|did not match)/i) | ||
) { | ||
const breadcrumb = createBreadcrumb({ | ||
category: 'replay.hydrate-error', | ||
}); | ||
addBreadcrumbEvent(replay, breadcrumb); | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
packages/replay/test/integration/coreHandlers/handleBeforeSendEvent.test.ts
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,80 @@ | ||
import { handleBeforeSendEvent } from '../../../src/coreHandlers/handleBeforeSendEvent'; | ||
import type { ReplayContainer } from '../../../src/replay'; | ||
import { Error } from '../../fixtures/error'; | ||
import { resetSdkMock } from '../../mocks/resetSdkMock'; | ||
import { useFakeTimers } from '../../utils/use-fake-timers'; | ||
|
||
useFakeTimers(); | ||
let replay: ReplayContainer; | ||
|
||
describe('Integration | coreHandlers | handleBeforeSendEvent', () => { | ||
afterEach(() => { | ||
replay.stop(); | ||
}); | ||
|
||
it('adds a hydration breadcrumb on development hydration error', async () => { | ||
({ replay } = await resetSdkMock({ | ||
replayOptions: { | ||
stickySession: false, | ||
}, | ||
sentryOptions: { | ||
replaysSessionSampleRate: 0.0, | ||
replaysOnErrorSampleRate: 1.0, | ||
}, | ||
})); | ||
|
||
const handler = handleBeforeSendEvent(replay); | ||
const addBreadcrumbSpy = jest.spyOn(replay, 'throttledAddEvent'); | ||
|
||
const error = Error(); | ||
error.exception.values[0].value = 'Text content did not match. Server: "A" Client: "B"'; | ||
handler(error); | ||
|
||
expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1); | ||
expect(addBreadcrumbSpy).toHaveBeenCalledWith({ | ||
data: { | ||
payload: { | ||
category: 'replay.hydrate-error', | ||
timestamp: expect.any(Number), | ||
type: 'default', | ||
}, | ||
tag: 'breadcrumb', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 5, | ||
}); | ||
}); | ||
|
||
it('adds a hydration breadcrumb on production hydration error', async () => { | ||
({ replay } = await resetSdkMock({ | ||
replayOptions: { | ||
stickySession: false, | ||
}, | ||
sentryOptions: { | ||
replaysSessionSampleRate: 0.0, | ||
replaysOnErrorSampleRate: 1.0, | ||
}, | ||
})); | ||
|
||
const handler = handleBeforeSendEvent(replay); | ||
const addBreadcrumbSpy = jest.spyOn(replay, 'throttledAddEvent'); | ||
|
||
const error = Error(); | ||
error.exception.values[0].value = 'https://reactjs.org/docs/error-decoder.html?invariant=423'; | ||
handler(error); | ||
|
||
expect(addBreadcrumbSpy).toHaveBeenCalledTimes(1); | ||
expect(addBreadcrumbSpy).toHaveBeenCalledWith({ | ||
data: { | ||
payload: { | ||
category: 'replay.hydrate-error', | ||
timestamp: expect.any(Number), | ||
type: 'default', | ||
}, | ||
tag: 'breadcrumb', | ||
}, | ||
timestamp: expect.any(Number), | ||
type: 5, | ||
}); | ||
}); | ||
}); |