Skip to content

Commit 937d13a

Browse files
author
Brian Vaughn
committed
Normalized stack format/display for different browsers
1 parent 2c64f1b commit 937d13a

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/renderers/shared/fiber/ReactFiberErrorLogger.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,25 @@ function logCapturedError(capturedError : CapturedError) : void {
2525
willRetry,
2626
} = capturedError;
2727

28-
const errorSummary = error.message
29-
? `Error: ${error.message}\n\n`
30-
: '';
28+
const {
29+
message,
30+
name,
31+
stack,
32+
} = error;
33+
34+
const errorSummary = message
35+
? `${name}: ${message}`
36+
: name;
3137

3238
const componentNameMessage = componentName
3339
? `React caught an error thrown by ${componentName}.`
3440
: 'React caught an error thrown by one of your components.';
3541

36-
const callStack = error.stack;
37-
const trimmedCallStack = callStack.substring(callStack.indexOf('\n'));
42+
// Error stack varies by browser; normalize it for our logging.
43+
const formattedCallStack = stack.replace(new RegExp(`^${errorSummary}\n`), '')
44+
.split('\n')
45+
.map((line) => `\n ${line.trim()}`)
46+
.join();
3847

3948
let errorBoundaryMessage;
4049
// errorBoundaryFound check is sufficient; errorBoundaryName check is to satisfy Flow.
@@ -56,9 +65,9 @@ function logCapturedError(capturedError : CapturedError) : void {
5665

5766
console.error(
5867
`${componentNameMessage} You should fix this error in your code. ${errorBoundaryMessage}\n\n` +
59-
errorSummary +
68+
`${errorSummary}\n\n` +
6069
`The error is located at: ${componentStack}\n\n` +
61-
`The error was thrown at: ${trimmedCallStack}`
70+
`The error was thrown at: ${formattedCallStack}`
6271
);
6372
}
6473

0 commit comments

Comments
 (0)