-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(node-core): Guard against undefined util.getSystemErrorMap #20660
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
141 changes: 141 additions & 0 deletions
141
packages/node-core/test/integrations/systemError.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,141 @@ | ||
| import type { Client, Event } from '@sentry/core'; | ||
| import type * as nodeUtil from 'node:util'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { systemErrorIntegration } from '../../src/integrations/systemError'; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getSystemErrorMap: vi.fn() as ReturnType<typeof vi.fn> | undefined, | ||
| })); | ||
|
|
||
| vi.mock('node:util', async importOriginal => { | ||
| const actual = (await importOriginal()) as typeof nodeUtil; | ||
| mocks.getSystemErrorMap = vi.fn(actual.getSystemErrorMap); | ||
| return { | ||
| ...actual, | ||
| get getSystemErrorMap() { | ||
| return mocks.getSystemErrorMap; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| import * as util from 'node:util'; | ||
|
|
||
| describe('systemErrorIntegration', () => { | ||
| afterEach(() => { | ||
| vi.mocked(util.getSystemErrorMap).mockRestore(); | ||
| }); | ||
|
|
||
| function createClient(sendDefaultPii = false): Client { | ||
| return { | ||
| getOptions: () => ({ sendDefaultPii }), | ||
| } as unknown as Client; | ||
| } | ||
|
|
||
| it('returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)', () => { | ||
| const originalFn = mocks.getSystemErrorMap; | ||
| mocks.getSystemErrorMap = undefined; | ||
|
|
||
| try { | ||
| const integration = systemErrorIntegration(); | ||
| const error = Object.assign(new Error('boom'), { errno: -2, path: '/some/path' }); | ||
| const event = { exception: { values: [{ value: error.message }] } } as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient()) as Event; | ||
|
|
||
| expect(result).toBe(event); | ||
| expect(result.contexts?.node_system_error).toBeUndefined(); | ||
| } finally { | ||
| mocks.getSystemErrorMap = originalFn; | ||
| } | ||
| }); | ||
|
|
||
| it('adds node_system_error context for a real SystemError', () => { | ||
| const errno = -2; | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[errno, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration(); | ||
| const error = Object.assign(new Error("ENOENT: no such file or directory, open '/secret/path'"), { | ||
| errno, | ||
| path: '/secret/path', | ||
| }); | ||
| const event = { exception: { values: [{ value: error.message }] } } as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient()) as Event; | ||
|
|
||
| expect(result.contexts?.node_system_error).toEqual({ errno }); | ||
| expect(result.exception?.values?.[0]?.value).not.toContain('/secret/path'); | ||
| }); | ||
|
|
||
| it('keeps path in context when sendDefaultPii is true', () => { | ||
| const errno = -2; | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[errno, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration(); | ||
| const error = Object.assign(new Error('boom'), { errno, path: '/secret/path' }); | ||
| const event = { exception: { values: [{ value: error.message }] } } as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient(true)) as Event; | ||
|
|
||
| expect(result.contexts?.node_system_error).toEqual({ errno, path: '/secret/path' }); | ||
| }); | ||
|
|
||
| it('keeps path in context when includePaths option is true', () => { | ||
| const errno = -2; | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[errno, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration({ includePaths: true }); | ||
| const error = Object.assign(new Error('boom'), { errno, path: '/secret/path' }); | ||
| const event = { exception: { values: [{ value: error.message }] } } as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient()) as Event; | ||
|
|
||
| expect(result.contexts?.node_system_error).toEqual({ errno, path: '/secret/path' }); | ||
| }); | ||
|
|
||
| it('returns the event unchanged when the error has no errno', () => { | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[-2, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration(); | ||
| const error = new Error('not a system error'); | ||
| const event = {} as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient()) as Event; | ||
|
|
||
| expect(result?.contexts?.node_system_error).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns the event unchanged when originalException is not an Error', () => { | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[-2, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration(); | ||
| const event = {} as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: 'not an error' }, createClient()) as Event; | ||
|
|
||
| expect(result.contexts?.node_system_error).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns the event unchanged when errno is not in the system error map', () => { | ||
| vi.mocked(util.getSystemErrorMap).mockReturnValue( | ||
| new Map<number, [string, string]>([[-2, ['ENOENT', 'no such file or directory']]]), | ||
| ); | ||
|
|
||
| const integration = systemErrorIntegration(); | ||
| const error = Object.assign(new Error('unknown'), { errno: 99999 }); | ||
| const event = {} as Event; | ||
|
|
||
| const result = integration.processEvent!(event, { originalException: error }, createClient()) as Event; | ||
|
|
||
| expect(result.contexts?.node_system_error).toBeUndefined(); | ||
| }); | ||
| }); |
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.