-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(effect): Capture errors through the Effect v4 ErrorReporter API #24151
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
Open
JPeer264
wants to merge
2
commits into
develop
Choose a base branch
from
jp/effect-4-error-reporter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
80 changes: 80 additions & 0 deletions
80
dev-packages/e2e-tests/test-applications/effect-4-node/tests/error-reporter.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,80 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('Captures an unhandled route failure through the HTTP reporting boundary', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('effect-4-node', event => { | ||
| return !event.type && event.exception?.values?.[0]?.value === 'User 42 could not be loaded'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/test-error-reporter/unhandled/42`); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
| const exception = errorEvent.exception?.values?.[0]; | ||
|
|
||
| expect(errorEvent.level).toBe('error'); | ||
| expect(exception).toMatchObject({ | ||
| type: 'Error', | ||
| value: 'User 42 could not be loaded', | ||
| mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, | ||
| }); | ||
|
|
||
| const frames = exception?.stacktrace?.frames ?? []; | ||
| const throwingFrame = frames[frames.length - 1]; | ||
| expect(throwingFrame).toMatchObject({ | ||
| function: 'loadUser', | ||
| filename: expect.stringMatching(/app\.js$/), | ||
| context_line: expect.stringContaining('could not be loaded'), | ||
| pre_context: expect.any(Array), | ||
| post_context: expect.any(Array), | ||
| }); | ||
| }); | ||
|
|
||
| test('Captures a failure reported with withErrorReporting before it is handled', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('effect-4-node', event => { | ||
| return !event.type && event.exception?.values?.[0]?.value === 'Handled after reporting'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/test-error-reporter/handled`); | ||
| const body = await response.json(); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(body).toEqual({ recovered: true }); | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Handled after reporting'); | ||
| }); | ||
|
|
||
| test('Skips errors annotated with ErrorReporter.ignore', async ({ baseURL }) => { | ||
| const ignoredEventPromise = waitForError('effect-4-node', event => { | ||
| return !event.type && event.exception?.values?.[0]?.type === 'NotFoundError'; | ||
| }).then(() => 'ignored error received'); | ||
|
|
||
| const sentinelEventPromise = waitForError('effect-4-node', event => { | ||
| return !event.type && event.exception?.values?.[0]?.value === 'User sentinel could not be loaded'; | ||
| }); | ||
|
|
||
| const ignoredResponse = await fetch(`${baseURL}/test-error-reporter/ignored`); | ||
| expect(ignoredResponse.status).toBe(500); | ||
|
|
||
| await fetch(`${baseURL}/test-error-reporter/unhandled/sentinel`); | ||
| await sentinelEventPromise; | ||
|
|
||
| // Events arrive in order, so once the sentinel is here an ignored event would already have resolved. | ||
| await expect(Promise.race([ignoredEventPromise, Promise.resolve('no ignored error')])).resolves.toBe( | ||
| 'no ignored error', | ||
| ); | ||
| }); | ||
|
|
||
| test('Applies the severity and attributes annotations', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('effect-4-node', event => { | ||
| return !event.type && event.exception?.values?.[0]?.type === 'RateLimitError'; | ||
| }); | ||
|
|
||
| await fetch(`${baseURL}/test-error-reporter/annotated`); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.level).toBe('warning'); | ||
| expect(errorEvent.extra).toEqual({ retryAfter: 60 }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type { SeverityLevel } from '@sentry/core'; | ||
| import { captureException, isObjectLike } from '@sentry/core'; | ||
| import * as Effect from 'effect'; | ||
| import type * as Cause from 'effect/Cause'; | ||
| import type * as EffectErrorReporter from 'effect/ErrorReporter'; | ||
| import type * as EffectLayer from 'effect/Layer'; | ||
| import { empty as emptyLayer } from 'effect/Layer'; | ||
| import type * as LogLevel from 'effect/LogLevel'; | ||
|
|
||
| // `effect/ErrorReporter` only exists in Effect v4, so it is read off the main entry instead of being imported | ||
| // as a subpath. On Effect v3 the lookup yields `undefined` and no reporter is registered. The property is read | ||
| // through a separate binding because bundlers fail the build on `Effect.ErrorReporter` when the export is absent. | ||
| const effectExports = Effect as Record<string, unknown>; | ||
| const ErrorReporter = effectExports.ErrorReporter as typeof EffectErrorReporter | undefined; | ||
|
|
||
| const SEVERITY_TO_LEVEL: Record<LogLevel.Severity, SeverityLevel> = { | ||
| Fatal: 'fatal', | ||
| Error: 'error', | ||
| Warn: 'warning', | ||
| Info: 'info', | ||
| Debug: 'debug', | ||
| Trace: 'debug', | ||
| }; | ||
|
|
||
| function getLevel(errorReporter: typeof EffectErrorReporter, error: unknown): SeverityLevel { | ||
| // Effect's `getSeverity` falls back to `Info` for unannotated errors, which would file plain failures as | ||
| // informational in Sentry. Only an explicit annotation changes the level. | ||
| if (isObjectLike(error) && errorReporter.severity in error) { | ||
| return SEVERITY_TO_LEVEL[errorReporter.getSeverity(error)]; | ||
| } | ||
| return 'error'; | ||
| } | ||
|
|
||
| function makeSentryErrorReporter(errorReporter: typeof EffectErrorReporter): EffectErrorReporter.ErrorReporter { | ||
| const reported = new WeakSet<object>(); | ||
|
|
||
| return { | ||
| [errorReporter.TypeId]: errorReporter.TypeId, | ||
| report({ cause }: { readonly cause: Cause.Cause<unknown> }): void { | ||
| if (reported.has(cause)) { | ||
| return; | ||
| } | ||
| reported.add(cause); | ||
|
|
||
| for (const reason of cause.reasons) { | ||
| if (reason._tag === 'Interrupt') { | ||
| continue; | ||
| } | ||
|
|
||
| // The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack, | ||
| // `cause` chain and error class. | ||
| const error = reason._tag === 'Fail' ? reason.error : reason.defect; | ||
|
|
||
| if (isObjectLike(error)) { | ||
| if (reported.has(error)) { | ||
| continue; | ||
| } | ||
| reported.add(error); | ||
| } | ||
|
|
||
| if (errorReporter.isIgnored(error)) { | ||
| continue; | ||
| } | ||
|
|
||
| captureException(error, { | ||
| mechanism: { type: 'auto.function.effect.error_reporter', handled: false }, | ||
| captureContext: { | ||
| level: getLevel(errorReporter, error), | ||
| extra: isObjectLike(error) ? { ...errorReporter.getAttributes(error) } : undefined, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Registers a Sentry `ErrorReporter` for `Effect.withErrorReporting`, `ErrorReporter.report` and the | ||
| * built-in HTTP and RPC reporting boundaries. Existing reporters are kept. | ||
| * | ||
| * Effect v3 has no `ErrorReporter` API, so the returned layer is empty there. | ||
| */ | ||
| export function makeSentryErrorReporterLayer(): EffectLayer.Layer<never, never, never> { | ||
| if (!ErrorReporter) { | ||
| return emptyLayer; | ||
| } | ||
|
|
||
| return ErrorReporter.layer([makeSentryErrorReporter(ErrorReporter)], { mergeWithExisting: 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
Oops, something went wrong.
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.