-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Add optional Sentry error reporting (env-driven, opt-in) #2139
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
kevincodex1
merged 5 commits into
Gitlawb:main
from
anushkadas-coder:fix/1777-sentry-reporting
Aug 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3199e79
Add optional Sentry error reporting (env-driven, opt-in)
anushkadas-coder e7fec01
Fix Sentry init to use dynamic import instead of require (ESM compati…
anushkadas-coder dd4ba7d
Document SENTRY_DSN setup in advanced-setup docs
anushkadas-coder 040f7f7
Disable Sentry default integrations; document runtime install require…
anushkadas-coder 6454dec
Wire reportErrorToSentry into top-level error handlers; add sentry.te…
anushkadas-coder 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,87 @@ | ||
| import { afterEach, beforeEach, expect, mock, test } from 'bun:test' | ||
|
|
||
| const ORIGINAL_SENTRY_DSN = process.env.SENTRY_DSN | ||
| const ORIGINAL_DISABLE_TELEMETRY = process.env.DISABLE_TELEMETRY | ||
|
|
||
| beforeEach(() => { | ||
| delete process.env.SENTRY_DSN | ||
| delete process.env.DISABLE_TELEMETRY | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| if (ORIGINAL_SENTRY_DSN === undefined) { | ||
| delete process.env.SENTRY_DSN | ||
| } else { | ||
| process.env.SENTRY_DSN = ORIGINAL_SENTRY_DSN | ||
| } | ||
| if (ORIGINAL_DISABLE_TELEMETRY === undefined) { | ||
| delete process.env.DISABLE_TELEMETRY | ||
| } else { | ||
| process.env.DISABLE_TELEMETRY = ORIGINAL_DISABLE_TELEMETRY | ||
| } | ||
| mock.restore() | ||
| }) | ||
|
|
||
| test('isSentryEnabled is false when SENTRY_DSN is unset', async () => { | ||
| const { isSentryEnabled } = await import('./sentry.js') | ||
| expect(isSentryEnabled()).toBe(false) | ||
| }) | ||
|
|
||
| test('isSentryEnabled is true when SENTRY_DSN is set and telemetry is not disabled', async () => { | ||
| process.env.SENTRY_DSN = 'https://example@o0.ingest.sentry.io/0' | ||
| const { isSentryEnabled } = await import('./sentry.js') | ||
| expect(isSentryEnabled()).toBe(true) | ||
| }) | ||
|
|
||
| test('isSentryEnabled is false when SENTRY_DSN is set but DISABLE_TELEMETRY is set', async () => { | ||
| process.env.SENTRY_DSN = 'https://example@o0.ingest.sentry.io/0' | ||
| process.env.DISABLE_TELEMETRY = '1' | ||
| const { isSentryEnabled } = await import('./sentry.js') | ||
| expect(isSentryEnabled()).toBe(false) | ||
| }) | ||
|
|
||
| test('reportErrorToSentry does not throw when Sentry is disabled', async () => { | ||
| const { reportErrorToSentry } = await import('./sentry.js') | ||
| const { TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS: TelemetrySafeError } = | ||
| await import('./errors.js') | ||
|
|
||
| expect(() => | ||
| reportErrorToSentry( | ||
| new TelemetrySafeError('full message with /some/file/path', 'sanitized message'), | ||
| ), | ||
| ).not.toThrow() | ||
| }) | ||
|
|
||
| test('reportErrorToSentry does not throw for a plain (non-sanitized) Error', async () => { | ||
| const { reportErrorToSentry } = await import('./sentry.js') | ||
|
|
||
| expect(() => | ||
| reportErrorToSentry(new Error('raw error with /some/file/path')), | ||
| ).not.toThrow() | ||
| }) | ||
|
|
||
| test('reportErrorToSentry only reports TelemetrySafeError, never a raw Error message', async () => { | ||
| process.env.SENTRY_DSN = 'https://example@o0.ingest.sentry.io/0' | ||
|
|
||
| const captureMessage = mock(() => {}) | ||
| mock.module('@sentry/node', () => ({ | ||
| init: mock(() => {}), | ||
| captureMessage, | ||
| })) | ||
|
|
||
| const { initializeSentry, reportErrorToSentry } = await import('./sentry.js') | ||
| const { TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS: TelemetrySafeError } = | ||
| await import('./errors.js') | ||
|
|
||
| await initializeSentry() | ||
|
|
||
| // A plain Error must never be reported — its message may contain file paths. | ||
| reportErrorToSentry(new Error('raw error with /some/file/path')) | ||
| expect(captureMessage).not.toHaveBeenCalled() | ||
|
|
||
| // A TelemetrySafeError reports only its sanitized telemetryMessage. | ||
| reportErrorToSentry( | ||
| new TelemetrySafeError('full message with /some/file/path', 'sanitized message'), | ||
| ) | ||
| expect(captureMessage).toHaveBeenCalledWith('sanitized message', 'error') | ||
| }) |
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,72 @@ | ||
| /** | ||
| * Optional, env-driven Sentry error reporting. | ||
| * | ||
| * Disabled by default. Enabled only when SENTRY_DSN is set AND telemetry | ||
| * is not disabled via DISABLE_TELEMETRY / CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC. | ||
| * | ||
| * Only TelemetrySafeError.telemetryMessage (never raw error.message) is sent, | ||
| * to avoid leaking file paths or other PII into Sentry. | ||
| */ | ||
| import { isTelemetryDisabled } from './privacyLevel.js' | ||
| import { TelemetrySafeError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS as TelemetrySafeError } from './errors.js' | ||
|
|
||
| let sentryInitialized = false | ||
| let sentryModule: typeof import('@sentry/node') | null = null | ||
|
|
||
| export function isSentryEnabled(): boolean { | ||
| return Boolean(process.env.SENTRY_DSN) && !isTelemetryDisabled() | ||
| } | ||
|
|
||
| /** | ||
| * Lazily initializes Sentry. No-op if SENTRY_DSN is unset or telemetry is disabled. | ||
| * Safe to call multiple times; only initializes once. Async because @sentry/node | ||
| * is loaded via dynamic import — this bundle is ESM and does not define require(). | ||
| */ | ||
| export async function initializeSentry(): Promise<void> { | ||
| if (sentryInitialized || !isSentryEnabled()) { | ||
| return | ||
| } | ||
| sentryInitialized = true | ||
|
|
||
| try { | ||
| // Dynamic import so @sentry/node is never loaded (or its startup cost | ||
| // paid) when the feature is off, and so it works under ESM where | ||
| // require() is not defined. | ||
| sentryModule = await import('@sentry/node') | ||
| sentryModule.init({ | ||
| dsn: process.env.SENTRY_DSN, | ||
| environment: process.env.NODE_ENV ?? 'production', | ||
| tracesSampleRate: 0, | ||
| // Disable Sentry's automatic uncaughtException/unhandledRejection | ||
| // integrations. Those hooks report raw error content, bypassing the | ||
| // TelemetrySafeError sanitization in reportErrorToSentry(). Only | ||
| // explicit reportErrorToSentry() calls should ever send data. | ||
| defaultIntegrations: false, | ||
| }) | ||
| } catch { | ||
| // Never let Sentry setup crash the CLI. | ||
| sentryModule = null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reports an error to Sentry if enabled. Only sends the sanitized | ||
| * telemetryMessage for TelemetrySafeError instances. Errors that are not | ||
| * TelemetrySafeError are NOT reported, since their raw message may contain | ||
| * file paths or other PII — never send an implicit raw error message. | ||
| */ | ||
| export function reportErrorToSentry(error: unknown): void { | ||
| if (!sentryModule || !isSentryEnabled()) { | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| if (error instanceof TelemetrySafeError) { | ||
| sentryModule.captureMessage(error.telemetryMessage, 'error') | ||
| } | ||
| // Non-TelemetrySafeError errors are intentionally not reported — their | ||
| // message has not been vetted as safe to send. | ||
| } catch { | ||
| // Reporting must never throw. | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
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.