Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/node-core/src/integrations/systemError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function isSystemError(error: unknown): error is SystemError {

// Appears this is the recommended way to check for Node.js SystemError
// https://github.com/nodejs/node/issues/46869
if (typeof util.getSystemErrorMap !== 'function') {
return false;
}
Comment thread
cursor[bot] marked this conversation as resolved.

return util.getSystemErrorMap().has(error.errno);
}

Expand Down
126 changes: 126 additions & 0 deletions packages/node-core/test/integrations/systemError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import * as util from 'node:util';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { systemErrorIntegration } from '../../src/integrations/systemError';

describe('systemErrorIntegration', () => {
const originalGetSystemErrorMap = util.getSystemErrorMap;

beforeEach(() => {
Object.defineProperty(util, 'getSystemErrorMap', {

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12

Check failure on line 9 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:9:12
value: originalGetSystemErrorMap,
configurable: true,
writable: true,
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test mock redefines module namespace

Medium Severity

util is an ESM module namespace for node:util, whose exports are non-configurable in Vitest’s Node environment. Redefining getSystemErrorMap with Object.defineProperty throws before assertions run, so the new regression tests fail instead of exercising the guard.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5d6a696. Configure here.

});

afterEach(() => {
Object.defineProperty(util, 'getSystemErrorMap', {

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (24) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (18) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (22) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when the error has no errno

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when includePaths option is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > keeps path in context when sendDefaultPii is true

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > adds node_system_error context for a real SystemError

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12

Check failure on line 17 in packages/node-core/test/integrations/systemError.test.ts

View workflow job for this annotation

GitHub Actions / Node (20) Unit Tests

test/integrations/systemError.test.ts > systemErrorIntegration > returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)

TypeError: Cannot redefine property: getSystemErrorMap ❯ test/integrations/systemError.test.ts:17:12
value: originalGetSystemErrorMap,
configurable: true,
writable: true,
});
});

function createClient(sendDefaultPii = false): any {
return { getOptions: () => ({ sendDefaultPii }) };
}

function setSystemErrorMap(value: unknown): void {
Object.defineProperty(util, 'getSystemErrorMap', {
value,
configurable: true,
writable: true,
});
}

it('returns the event unchanged when util.getSystemErrorMap is undefined (e.g. Bun)', () => {
setSystemErrorMap(undefined);

const integration = systemErrorIntegration();
const error = Object.assign(new Error('boom'), { errno: -2, path: '/some/path' });
const event: any = { exception: { values: [{ value: error.message }] } };

const result = integration.processEvent!(event, { originalException: error }, createClient());

expect(result).toBe(event);
expect(result.contexts?.node_system_error).toBeUndefined();
});

it('adds node_system_error context for a real SystemError', () => {
const errno = -2;
setSystemErrorMap(() => 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: any = { exception: { values: [{ value: error.message }] } };

const result = integration.processEvent!(event, { originalException: error }, createClient());

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;
setSystemErrorMap(() => 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: any = { exception: { values: [{ value: error.message }] } };

const result = integration.processEvent!(event, { originalException: error }, createClient(true));

expect(result.contexts?.node_system_error).toEqual({ errno, path: '/secret/path' });
});

it('keeps path in context when includePaths option is true', () => {
const errno = -2;
setSystemErrorMap(() => 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: any = { exception: { values: [{ value: error.message }] } };

const result = integration.processEvent!(event, { originalException: error }, createClient());

expect(result.contexts?.node_system_error).toEqual({ errno, path: '/secret/path' });
});

it('returns the event unchanged when the error has no errno', () => {
setSystemErrorMap(() => 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: any = {};

const result = integration.processEvent!(event, { originalException: error }, createClient());

expect(result.contexts?.node_system_error).toBeUndefined();
});

it('returns the event unchanged when originalException is not an Error', () => {
setSystemErrorMap(() => new Map<number, [string, string]>([[-2, ['ENOENT', 'no such file or directory']]]));

const integration = systemErrorIntegration();
const event: any = {};

const result = integration.processEvent!(event, { originalException: 'not an error' }, createClient());

expect(result.contexts?.node_system_error).toBeUndefined();
});

it('returns the event unchanged when errno is not in the system error map', () => {
setSystemErrorMap(() => 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: any = {};

const result = integration.processEvent!(event, { originalException: error }, createClient());

expect(result.contexts?.node_system_error).toBeUndefined();
});
});
Loading