Skip to content

fix(integrations): Handle windows paths with no prefix or backslash prefix in RewriteFrames #7995

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 8 commits into from
May 3, 2023
8 changes: 6 additions & 2 deletions packages/integrations/src/rewriteframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ export class RewriteFrames implements Integration {
if (!frame.filename) {
return frame;
}
// Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\`
const isWindowsFrame = /^[a-zA-Z]:\\/.test(frame.filename);
// Determine if this is a Windows frame by checking for a Windows-style prefix such as `C:\`
const isWindowsFrame =
/^[a-zA-Z]:\\/.test(frame.filename) ||
// or the presence of a backslash without a forward slash (which are not allowed on Windows)
(frame.filename.includes('\\') && !frame.filename.includes('/'));
// Check if the frame filename begins with `/`
const startsWithSlash = /^\//.test(frame.filename);
if (isWindowsFrame || startsWithSlash) {
const filename = isWindowsFrame
Expand Down
52 changes: 50 additions & 2 deletions packages/integrations/test/rewriteframes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let exceptionEvent: Event;
let exceptionWithoutStackTrace: Event;
let windowsExceptionEvent: Event;
let windowsLowerCaseExceptionEvent: Event;
let windowsExceptionEventWithoutPrefix: Event;
let windowsExceptionEventWithBackslashPrefix: Event;
let multipleStacktracesEvent: Event;

describe('RewriteFrames', () => {
Expand Down Expand Up @@ -44,6 +46,28 @@ describe('RewriteFrames', () => {
],
},
};
windowsExceptionEventWithoutPrefix = {
exception: {
values: [
{
stacktrace: {
frames: [{ filename: 'www\\src\\app\\file1.js' }, { filename: 'www\\src\\app\\file2.js' }],
},
},
],
},
};
windowsExceptionEventWithBackslashPrefix = {
exception: {
values: [
{
stacktrace: {
frames: [{ filename: '\\www\\src\\app\\file1.js' }, { filename: '\\www\\src\\app\\file2.js' }],
},
},
],
},
};
exceptionWithoutStackTrace = {
exception: {
values: [{}],
Expand Down Expand Up @@ -121,6 +145,18 @@ describe('RewriteFrames', () => {
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///file2.js');
});

it('transforms windowsExceptionEvent frames with no prefix', () => {
const event = rewriteFrames.process(windowsExceptionEventWithoutPrefix);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///file2.js');
});

it('transforms windowsExceptionEvent frames with backslash prefix', () => {
const event = rewriteFrames.process(windowsExceptionEventWithBackslashPrefix);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///file2.js');
});
});

describe('can use custom root to perform `relative` on filepaths', () => {
Expand All @@ -136,17 +172,29 @@ describe('RewriteFrames', () => {
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/mo\\dule/file2.js');
});

it('trasforms windowsExceptionEvent frames', () => {
it('transforms windowsExceptionEvent frames', () => {
const event = rewriteFrames.process(windowsExceptionEvent);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
});

it('trasforms windowsExceptionEvent lower-case prefix frames', () => {
it('transforms windowsExceptionEvent lower-case prefix frames', () => {
const event = rewriteFrames.process(windowsLowerCaseExceptionEvent);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
});

it('transforms windowsExceptionEvent frames with no prefix', () => {
const event = rewriteFrames.process(windowsExceptionEventWithoutPrefix);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
});

it('transforms windowsExceptionEvent frames with backslash prefix', () => {
const event = rewriteFrames.process(windowsExceptionEventWithBackslashPrefix);
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
});
});

describe('can use custom iteratee', () => {
Expand Down