Skip to content

Commit 2979b2c

Browse files
authored
feat: Use framesToPop for InvaliantViolations in React errors (#2204)
1 parent 01bab2d commit 2979b2c

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- [browser] feat: Use framesToPop for InvaliantViolations in React errors (#2204)
6+
37
## 5.6.1
48

59
- [core] fix: Correctly detect when client is enabled before installing integrations (#2193)

packages/browser/src/tracekit.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ TraceKit._computeStackTrace = (function _computeStackTraceWrapper() {
870870
*/
871871
function computeStackTrace(ex: any, depth: any) {
872872
var stack = null;
873+
var popSize = ex && ex.framesToPop;
873874
depth = depth == null ? 0 : +depth;
874875

875876
try {
@@ -878,28 +879,28 @@ TraceKit._computeStackTrace = (function _computeStackTraceWrapper() {
878879
// property first!!
879880
stack = _computeStackTraceFromStacktraceProp(ex);
880881
if (stack) {
881-
return stack;
882+
return popFrames(stack, popSize);
882883
}
883884
} catch (e) {}
884885

885886
try {
886887
stack = _computeStackTraceFromStackProp(ex);
887888
if (stack) {
888-
return stack;
889+
return popFrames(stack, popSize);
889890
}
890891
} catch (e) {}
891892

892893
try {
893894
stack = _computeStackTraceFromOperaMultiLineMessage(ex);
894895
if (stack) {
895-
return stack;
896+
return popFrames(stack, popSize);
896897
}
897898
} catch (e) {}
898899

899900
try {
900901
stack = _computeStackTraceByWalkingCallerChain(ex, depth + 1);
901902
if (stack) {
902-
return stack;
903+
return popFrames(stack, popSize);
903904
}
904905
} catch (e) {}
905906

@@ -911,6 +912,21 @@ TraceKit._computeStackTrace = (function _computeStackTraceWrapper() {
911912
};
912913
}
913914

915+
function popFrames(stacktrace: any, popSize: number): any {
916+
if (Number.isNaN(popSize)) {
917+
return stacktrace;
918+
}
919+
920+
try {
921+
return {
922+
...stacktrace,
923+
stack: stacktrace.stack.slice(popSize),
924+
};
925+
} catch (e) {
926+
return stacktrace;
927+
}
928+
}
929+
914930
(computeStackTrace as any)._augmentStackTraceWithInitialElement = _augmentStackTraceWithInitialElement;
915931
(computeStackTrace as any)._computeStackTraceFromStackProp = _computeStackTraceFromStackProp;
916932

packages/browser/test/unit/tracekit/custom.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,57 @@ describe('Tracekit - Custom Tests', () => {
284284
]);
285285
});
286286
});
287+
288+
describe('React', () => {
289+
it('should correctly parse Invariant Violation errors and use framesToPop to drop info message', () => {
290+
const REACT_INVARIANT_VIOLATION_EXCEPTION = {
291+
framesToPop: 1,
292+
message:
293+
'Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%7D&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings. ',
294+
name: 'Invariant Violation',
295+
stack: `Invariant Violation: Minified React error #31; visit https://reactjs.org/docs/error-decoder.html?invariant=31&args[]=object%20with%20keys%20%7B%7D&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
296+
at http://localhost:5000/static/js/foo.chunk.js:1:21738
297+
at a (http://localhost:5000/static/js/foo.chunk.js:1:21841)
298+
at ho (http://localhost:5000/static/js/foo.chunk.js:1:68735)
299+
at f (http://localhost:5000/:1:980)`,
300+
};
301+
302+
const stacktrace = _computeStackTrace(REACT_INVARIANT_VIOLATION_EXCEPTION);
303+
304+
expect(stacktrace.stack).deep.equal([
305+
{
306+
args: [],
307+
column: 21738,
308+
context: null,
309+
func: '?',
310+
line: 1,
311+
url: 'http://localhost:5000/static/js/foo.chunk.js',
312+
},
313+
{
314+
args: [],
315+
column: 21841,
316+
context: null,
317+
func: 'a',
318+
line: 1,
319+
url: 'http://localhost:5000/static/js/foo.chunk.js',
320+
},
321+
{
322+
args: [],
323+
column: 68735,
324+
context: null,
325+
func: 'ho',
326+
line: 1,
327+
url: 'http://localhost:5000/static/js/foo.chunk.js',
328+
},
329+
{
330+
args: [],
331+
column: 980,
332+
context: null,
333+
func: 'f',
334+
line: 1,
335+
url: 'http://localhost:5000/',
336+
},
337+
]);
338+
});
339+
});
287340
});

0 commit comments

Comments
 (0)