Skip to content

Commit c2fa418

Browse files
Nick Frasserkamilogorek
authored andcommitted
feat: Support Windows-style path in RewriteFrame iteratee
The previous default iteratee does not work in Windows because it only detects paths that begin with `/`. This change ensures paths that begin with `C:\\` are also accounted for. This implementation requires that the specified `root` uses Unix-style paths. So this will not work: new RewriteFrames({ root: 'C:\\Program Files\\Apache\\www' }) But this will: new RewriteFrames({ root: '/Program Files/Apache/www' }) Should this behaviour change or be noted in the official docs?
1 parent c565329 commit c2fa418

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/integrations/src/rewriteframes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ export class RewriteFrames implements Integration {
2424
* @inheritDoc
2525
*/
2626
private readonly _iteratee: StackFrameIteratee = (frame: StackFrame) => {
27-
if (frame.filename && frame.filename.startsWith('/')) {
28-
const base = this._root ? relative(this._root, frame.filename) : basename(frame.filename);
27+
// Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\\`
28+
if (frame.filename && /^(\/|[A-Z]:\\)/.test(frame.filename)) {
29+
const filename = frame.filename
30+
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
31+
.replace(/\\/g, '/'); // replace all `\\` instances with `/`
32+
const base = this._root ? relative(this._root, filename) : basename(filename);
2933
frame.filename = `app:///${base}`;
3034
}
3135
return frame;

packages/integrations/test/rewriteframes.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RewriteFrames } from '../src/rewriteframes';
55
let rewriteFrames: RewriteFrames;
66
let messageEvent: Event;
77
let exceptionEvent: Event;
8+
let windowsExceptionEvent: Event;
89

910
describe('RewriteFrames', () => {
1011
beforeEach(() => {
@@ -38,6 +39,24 @@ describe('RewriteFrames', () => {
3839
],
3940
},
4041
};
42+
windowsExceptionEvent = {
43+
exception: {
44+
values: [
45+
{
46+
stacktrace: {
47+
frames: [
48+
{
49+
filename: 'C:\\www\\src\\app\\file1.js',
50+
},
51+
{
52+
filename: 'C:\\www\\src\\app\\file2.js',
53+
},
54+
],
55+
},
56+
},
57+
],
58+
},
59+
};
4160
});
4261

4362
describe('default iteratee appends basename to `app:///` if frame starts with `/`', () => {
@@ -58,6 +77,18 @@ describe('RewriteFrames', () => {
5877
});
5978
});
6079

80+
describe('default iteratee appends basename to `app:///` if frame starts with `C:\\`', () => {
81+
beforeEach(() => {
82+
rewriteFrames = new RewriteFrames();
83+
});
84+
85+
it('trasforms windowsExceptionEvent frames', () => {
86+
const event = rewriteFrames.process(windowsExceptionEvent);
87+
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///file1.js');
88+
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///file2.js');
89+
});
90+
});
91+
6192
describe('can use custom root to perform `relative` on filepaths', () => {
6293
beforeEach(() => {
6394
rewriteFrames = new RewriteFrames({
@@ -76,6 +107,12 @@ describe('RewriteFrames', () => {
76107
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
77108
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
78109
});
110+
111+
it('trasforms windowsExceptionEvent frames', () => {
112+
const event = rewriteFrames.process(windowsExceptionEvent);
113+
expect(event.exception!.values![0].stacktrace!.frames![0].filename).toEqual('app:///src/app/file1.js');
114+
expect(event.exception!.values![0].stacktrace!.frames![1].filename).toEqual('app:///src/app/file2.js');
115+
});
79116
});
80117

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

0 commit comments

Comments
 (0)