Created
January 31, 2019 11:49
-
-
Save loretoparisi/b83d507230a5296806308a561ae8c800 to your computer and use it in GitHub Desktop.
React DOM Error logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This module is forked in different environments. | |
// By default, return `true` to log errors to the console. | |
// Forks can return `false` if this isn't desirable. | |
function showErrorDialog(capturedError) { | |
return true; | |
} | |
function logCapturedError(capturedError) { | |
var logError = showErrorDialog(capturedError); | |
// Allow injected showErrorDialog() to prevent default console.error logging. | |
// This enables renderers like ReactNative to better manage redbox behavior. | |
if (logError === false) { | |
return; | |
} | |
var error = capturedError.error; | |
var suppressLogging = error && error.suppressReactErrorLogging; | |
if (suppressLogging) { | |
return; | |
} | |
{ | |
var componentName = capturedError.componentName, | |
componentStack = capturedError.componentStack, | |
errorBoundaryName = capturedError.errorBoundaryName, | |
errorBoundaryFound = capturedError.errorBoundaryFound, | |
willRetry = capturedError.willRetry; | |
var componentNameMessage = componentName ? 'The above error occurred in the <' + componentName + '> component:' : 'The above error occurred in one of your React components:'; | |
var errorBoundaryMessage = void 0; | |
// errorBoundaryFound check is sufficient; errorBoundaryName check is to satisfy Flow. | |
if (errorBoundaryFound && errorBoundaryName) { | |
if (willRetry) { | |
errorBoundaryMessage = 'React will try to recreate this component tree from scratch ' + ('using the error boundary you provided, ' + errorBoundaryName + '.'); | |
} else { | |
errorBoundaryMessage = 'This error was initially handled by the error boundary ' + errorBoundaryName + '.\n' + 'Recreating the tree from scratch failed so React will unmount the tree.'; | |
} | |
} else { | |
errorBoundaryMessage = 'Consider adding an error boundary to your tree to customize error handling behavior.\n' + 'Visit https://fb.me/react-error-boundaries to learn more about error boundaries.'; | |
} | |
var combinedMessage = '' + componentNameMessage + componentStack + '\n\n' + ('' + errorBoundaryMessage); | |
// In development, we provide our own message with just the component stack. | |
// We don't include the original error message and JS stack because the browser | |
// has already printed it. Even if the application swallows the error, it is still | |
// displayed by the browser thanks to the DEV-only fake event trick in ReactErrorUtils. | |
console.error(combinedMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment