-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JS Error Tracking: Allow custom error reporting logic to be called in…
… Error Boundaries via a WP action hook (#42024) * Call WP action hook that allow other consuming code to set a custom additional error reporting logic for Error Boundaries * Remove doAction from edit navigation error boundary Separate navigation editor screen development is on hold. * Rename action * Add unit tests * Document the new action * Update editor-actions.md
- Loading branch information
1 parent
64baf53
commit d68dedb
Showing
8 changed files
with
152 additions
and
0 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,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