-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
JS Error Tracking: Allow custom error reporting logic to be called in Error Boundaries via a WP action hook #42024
Merged
gziolo
merged 6 commits into
trunk
from
add/error-reporting-action-hooks-to-error-boundaries
Jul 12, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0add211
Call WP action hook that allow other consuming code to set a custom a…
fullofcaffeine f8e14dc
Remove doAction from edit navigation error boundary
fullofcaffeine ee5398a
Rename action
fullofcaffeine 32671b5
Add unit tests
fullofcaffeine 6159e86
Document the new action
fullofcaffeine 8f2a522
Update editor-actions.md
fullofcaffeine 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Editor Actions | ||
|
||
To help you hook into the editor lifecycle and extend it, the following Actions are exposed: | ||
|
||
### Error Boundaries | ||
|
||
#### `editor.ErrorBoundary.errorLogged` | ||
|
||
Allows you to hook into the editor Error Boundaries' `componentDidCatch` and gives you access to the error object. | ||
|
||
You can use if you want to get hold of the error object that's handled by the boundaries, i.e to send them to an external error tracking tool. | ||
|
||
_Example_: | ||
|
||
```js | ||
addAction( | ||
'editor.ErrorBoundary.errorLogged', | ||
'mu-plugin/error-capture-setup', | ||
( error ) => { | ||
// error is the exception's error object | ||
ErrorCaptureTool.captureError( error ); | ||
} | ||
); | ||
``` |
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
38 changes: 38 additions & 0 deletions
38
packages/customize-widgets/src/components/test/error-boundary.js
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,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import * as wpHooks from '@wordpress/hooks'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ErrorBoundary from '../error-boundary'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
const theError = new Error( 'Kaboom' ); | ||
|
||
const ChildComponent = () => { | ||
throw theError; | ||
}; | ||
|
||
describe( 'Error Boundary', () => { | ||
describe( 'when error is thrown from a Child component', () => { | ||
it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { | ||
const doAction = jest.spyOn( wpHooks, 'doAction' ); | ||
|
||
render( | ||
<ErrorBoundary> | ||
<ChildComponent /> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect( doAction ).toHaveBeenCalledWith( | ||
'editor.ErrorBoundary.errorLogged', | ||
theError | ||
); | ||
expect( console ).toHaveErrored(); | ||
} ); | ||
} ); | ||
} ); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import * as wpHooks from '@wordpress/hooks'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ErrorBoundary from '../error-boundary'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
const theError = new Error( 'Kaboom' ); | ||
|
||
const ChildComponent = () => { | ||
throw theError; | ||
}; | ||
|
||
describe( 'Error Boundary', () => { | ||
describe( 'when error is thrown from a Child component', () => { | ||
it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { | ||
const doAction = jest.spyOn( wpHooks, 'doAction' ); | ||
|
||
render( | ||
<ErrorBoundary> | ||
<ChildComponent /> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect( doAction ).toHaveBeenCalledWith( | ||
'editor.ErrorBoundary.errorLogged', | ||
theError | ||
); | ||
expect( console ).toHaveErrored(); | ||
} ); | ||
} ); | ||
} ); |
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
38 changes: 38 additions & 0 deletions
38
packages/edit-widgets/src/components/test/error-boundary.js
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,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import * as wpHooks from '@wordpress/hooks'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ErrorBoundary from '../error-boundary'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
const theError = new Error( 'Kaboom' ); | ||
|
||
const ChildComponent = () => { | ||
throw theError; | ||
}; | ||
|
||
describe( 'Error Boundary', () => { | ||
describe( 'when error is thrown from a Child component', () => { | ||
it( 'calls the `editor.ErrorBoundary.errorLogged` hook action with the error object', () => { | ||
const doAction = jest.spyOn( wpHooks, 'doAction' ); | ||
|
||
render( | ||
<ErrorBoundary> | ||
<ChildComponent /> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect( doAction ).toHaveBeenCalledWith( | ||
'editor.ErrorBoundary.errorLogged', | ||
theError | ||
); | ||
expect( console ).toHaveErrored(); | ||
} ); | ||
} ); | ||
} ); |
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
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.
The unit-tests are identical but simple enough, but there's one for each error boundary, which is a symptom of the existing unDRYness for Error Boundaries. We can turn them into a single test once we work on #42024 (comment).
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.
Since we plan to work on #42024 (comment) soon, I'll abstain from trying to DRY the unit-tests (by extracting the common logic into a shared function or something along those lines, for example).