-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CPLAT-7721 Add error logging to ErrorBoundary #370
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be378d7
Add error logging to ErrorBoundary
aaronlademann-wf a40849b
Add error info to log message
aaronlademann-wf 7d56418
Don’t cache logger
aaronlademann-wf 0681787
Make ErrorBoundary logging more flexible for consumers
aaronlademann-wf c9ca115
Address CR feedback
aaronlademann-wf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,8 +167,19 @@ class _$ErrorBoundaryProps extends UiProps { | |
|
||
/// The name to use when the component's logger logs an error via [ErrorBoundaryComponent.componentDidCatch]. | ||
/// | ||
/// Not used if a custom [logger] is specified. | ||
/// | ||
/// > Default: 'over_react.ErrorBoundary' | ||
String loggerName; | ||
|
||
/// Whether errors caught by this [ErrorBoundary] should be logged using a [Logger]. | ||
/// | ||
/// > Default: `true` | ||
bool shouldLogErrors; | ||
|
||
/// An optional custom logger instance that will be used to log errors caught by | ||
/// this [ErrorBoundary] when [shouldLogErrors] is true. | ||
Logger logger; | ||
} | ||
|
||
@State() | ||
|
@@ -199,6 +210,7 @@ class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBounda | |
Map getDefaultProps() => (newProps() | ||
..identicalErrorFrequencyTolerance = Duration(seconds: 5) | ||
..loggerName = defaultErrorBoundaryLoggerName | ||
..shouldLogErrors = true | ||
); | ||
|
||
@override | ||
|
@@ -207,33 +219,6 @@ class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBounda | |
..showFallbackUIOnError = props.fallbackUIRenderer != null | ||
); | ||
|
||
@mustCallSuper | ||
@override | ||
void componentWillMount() { | ||
super.componentWillMount(); | ||
|
||
_logger = Logger(_getLoggerName()); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void componentWillUnmount() { | ||
super.componentWillUnmount(); | ||
|
||
_logger.clearListeners(); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void componentWillReceiveProps(Map nextProps) { | ||
super.componentWillReceiveProps(nextProps); | ||
|
||
final tNextProps = typedPropsFactory(nextProps); | ||
if (tNextProps.loggerName != props.loggerName) { | ||
_logger = Logger(_getLoggerName(tNextProps)); | ||
} | ||
} | ||
|
||
@mustCallSuper | ||
/*@override*/ | ||
S getDerivedStateFromError(_) { | ||
|
@@ -334,7 +319,7 @@ class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBounda | |
// wrap in a try catch just in case `findDomNode` throws as a result of the | ||
// wrapped react tree rendering a string instead of a composite or dom component. | ||
// | ||
// [3] Log the caught error using the instance Logger. | ||
// [3] Log the caught error using a logger if `props.shouldLogErrors` is true. | ||
// ---------------------------------------------- /\ ---------------------------------------------- | ||
|
||
String _domAtTimeOfError; | ||
|
@@ -428,27 +413,24 @@ class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBounda | |
String _getReadableErrorInfo(/*NativeJavascriptObject*/dynamic jsErrorInfo) => | ||
getProperty(jsErrorInfo, 'componentStack'); | ||
|
||
/// The logger that logs errors when a component somewhere within the React tree wrapped by | ||
/// this [ErrorBoundary] instance throws within the React lifecycle. | ||
Logger get logger => _logger; | ||
Logger _logger; | ||
/// The value that will be used when creating a [Logger] to log errors from this component. | ||
String get loggerName { | ||
if (props.logger != null) return props.logger.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to the above comment; if this isn't public, this won't be necessary. |
||
|
||
String _getLoggerName([T propsMap]) { | ||
propsMap ??= props; | ||
if (propsMap.loggerName != null && propsMap.loggerName.isNotEmpty) return propsMap.loggerName; | ||
|
||
return defaultErrorBoundaryLoggerName; | ||
return props.loggerName ?? defaultErrorBoundaryLoggerName; | ||
} | ||
|
||
// ----- [3] ----- // | ||
void _logErrorCaughtByErrorBoundary( | ||
/*Error|Exception*/ dynamic error, | ||
/*ReactErrorInfo*/ dynamic info, { | ||
/*ReactErrorInfo*/ String info, { | ||
bool isRecoverable = true, | ||
}) { | ||
if (!props.shouldLogErrors) return; | ||
|
||
String message = isRecoverable | ||
? 'An error was caught by an ErrorBoundary' | ||
: 'An unrecoverable error was caught by an ErrorBoundary (the entire react tree had to be unmounted)'; | ||
? 'An error was caught by an ErrorBoundary: \nInfo: $info' | ||
: 'An unrecoverable error was caught by an ErrorBoundary (attempting to remount it was unsuccessful): \nInfo: $info'; | ||
|
||
dynamic stackTrace; | ||
try { | ||
|
@@ -457,6 +439,6 @@ class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBounda | |
// The error / exception doesn't extend from Error or Exception | ||
} | ||
|
||
_logger.severe(message, error, stackTrace); | ||
(props.logger ?? Logger(loggerName)).severe(message, error, stackTrace); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a use-case for making this public? Consumers know which logger/loggerName they passed in; we shouldn't need to expose that info via an API method.