-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[base] Add less intrusive error handler (#1367)
- Loading branch information
Showing
3 changed files
with
75 additions
and
8 deletions.
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react' | ||
import Snackbar from 'part:@sanity/components/snackbar/default' | ||
|
||
const SANITY_ERROR_HANDLER = Symbol.for('SANITY_ERROR_HANDLER') | ||
|
||
export default class ErrorHandler extends React.PureComponent { | ||
state = {error: null} | ||
|
||
constructor(props) { | ||
super(props) | ||
|
||
this.handleGlobalError = this.handleGlobalError.bind(this) | ||
this.handleGlobalError.identity = SANITY_ERROR_HANDLER | ||
} | ||
|
||
componentDidMount() { | ||
// Only store the original error handler if it wasn't a copy of _this_ error handler | ||
if (window.onerror && window.onerror.identity !== SANITY_ERROR_HANDLER) { | ||
this.originalErrorHandler = window.onerror | ||
} | ||
|
||
window.onerror = this.handleGlobalError | ||
} | ||
|
||
componentWillUnmount() { | ||
window.onerror = this.originalErrorHandler || window.onerror | ||
} | ||
|
||
handleGlobalError = (msg, url, lineNo, columnNo, err) => { | ||
// Certain events (ResizeObserver max loop threshold, for instance) | ||
// only gives a _message_. We choose to ignore these events since | ||
// they are usually not _fatal_ | ||
if (!err) { | ||
return | ||
} | ||
|
||
// Certain errors should be ignored | ||
if ( | ||
[ | ||
/unexpected token <$/i // Trying to load HTML as JS | ||
].some(item => item.test(err.message)) | ||
) { | ||
return | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
this.setState({error: err}) | ||
} | ||
|
||
handleClose = () => { | ||
this.setState({error: null}) | ||
} | ||
|
||
render() { | ||
const {error} = this.state | ||
if (!error) { | ||
return null | ||
} | ||
|
||
const message = __DEV__ ? `An error occured: ${error.message}` : 'An error occured' | ||
|
||
return ( | ||
<Snackbar kind="error" action={{title: 'Close'}} onAction={this.handleClose} timeout={2500}> | ||
<div> | ||
<strong>{message}</strong> | ||
</div> | ||
<div>Check browser javascript console for details</div> | ||
</Snackbar> | ||
) | ||
} | ||
} |
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 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