mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Tweaked captured error log slightly based on feedback (#8785)
Tweaked captured error log slightly based on feedback. Normalized stack format/display for different browsers
This commit is contained in:
@@ -25,10 +25,33 @@ function logCapturedError(capturedError : CapturedError) : void {
|
||||
willRetry,
|
||||
} = capturedError;
|
||||
|
||||
const {
|
||||
message,
|
||||
name,
|
||||
stack,
|
||||
} = error;
|
||||
|
||||
const errorSummary = message
|
||||
? `${name}: ${message}`
|
||||
: name;
|
||||
|
||||
const componentNameMessage = componentName
|
||||
? `React caught an error thrown by ${componentName}.`
|
||||
: 'React caught an error thrown by one of your components.';
|
||||
|
||||
// Error stack varies by browser, eg:
|
||||
// Chrome prepends the Error name and type.
|
||||
// Firefox, Safari, and IE don't indent the stack lines.
|
||||
// Format it in a consistent way for error logging.
|
||||
let formattedCallStack = stack.slice(0, errorSummary.length) === errorSummary
|
||||
? stack.slice(errorSummary.length)
|
||||
: stack;
|
||||
formattedCallStack = formattedCallStack
|
||||
.trim()
|
||||
.split('\n')
|
||||
.map((line) => `\n ${line.trim()}`)
|
||||
.join();
|
||||
|
||||
let errorBoundaryMessage;
|
||||
// errorBoundaryFound check is sufficient; errorBoundaryName check is to satisfy Flow.
|
||||
if (errorBoundaryFound && errorBoundaryName) {
|
||||
@@ -49,8 +72,9 @@ function logCapturedError(capturedError : CapturedError) : void {
|
||||
|
||||
console.error(
|
||||
`${componentNameMessage} You should fix this error in your code. ${errorBoundaryMessage}\n\n` +
|
||||
`${error.stack}\n\n` +
|
||||
`The error was thrown in the following location: ${componentStack}`
|
||||
`${errorSummary}\n\n` +
|
||||
`The error is located at: ${componentStack}\n\n` +
|
||||
`The error was thrown at: ${formattedCallStack}`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -980,7 +980,7 @@ describe('ReactIncrementalErrorHandling', () => {
|
||||
);
|
||||
expect(errorMessage).toContain('Error: componentWillMount error');
|
||||
expect(normalizeCodeLocInfo(errorMessage)).toContain(
|
||||
'The error was thrown in the following location: \n' +
|
||||
'The error is located at: \n' +
|
||||
' in ErrorThrowingComponent (at **)\n' +
|
||||
' in span (at **)\n' +
|
||||
' in div (at **)'
|
||||
@@ -1014,7 +1014,7 @@ describe('ReactIncrementalErrorHandling', () => {
|
||||
);
|
||||
expect(errorMessage).toContain('Error: componentDidMount error');
|
||||
expect(normalizeCodeLocInfo(errorMessage)).toContain(
|
||||
'The error was thrown in the following location: \n' +
|
||||
'The error is located at: \n' +
|
||||
' in ErrorThrowingComponent (at **)\n' +
|
||||
' in span (at **)\n' +
|
||||
' in div (at **)'
|
||||
|
||||
Reference in New Issue
Block a user