Skip to content
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
merged 6 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';

function CopyButton( { text, children } ) {
const ref = useCopyToClipboard( text );
Expand All @@ -26,6 +27,8 @@ export default class ErrorBoundary extends Component {

componentDidCatch( error ) {
this.setState( { error } );

doAction( 'editor.ErrorBoundary.errorLogged', error );
}

render() {
Expand Down
38 changes: 38 additions & 0 deletions packages/customize-widgets/src/components/test/error-boundary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
Copy link
Member Author

@fullofcaffeine fullofcaffeine Jul 8, 2022

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).

Copy link
Member Author

@fullofcaffeine fullofcaffeine Jul 8, 2022

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).

* 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();
} );
} );
} );
5 changes: 5 additions & 0 deletions packages/edit-site/src/components/error-boundary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { doAction } from '@wordpress/hooks';

/**
* Internal dependencies
Expand All @@ -20,6 +21,10 @@ export default class ErrorBoundary extends Component {
};
}

componentDidCatch( error ) {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
doAction( 'editor.ErrorBoundary.errorLogged', error );
}

static getDerivedStateFromError( error ) {
return { error };
}
Expand Down
38 changes: 38 additions & 0 deletions packages/edit-site/src/components/test/error-boundary.js
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();
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';

function CopyButton( { text, children } ) {
const ref = useCopyToClipboard( text );
Expand All @@ -29,6 +30,8 @@ export default class ErrorBoundary extends Component {

componentDidCatch( error ) {
this.setState( { error } );

doAction( 'editor.ErrorBoundary.errorLogged', error );
}

reboot() {
Expand Down
38 changes: 38 additions & 0 deletions packages/edit-widgets/src/components/test/error-boundary.js
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();
} );
} );
} );
3 changes: 3 additions & 0 deletions packages/editor/src/components/error-boundary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Button } from '@wordpress/components';
import { select } from '@wordpress/data';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';

/**
* Internal dependencies
Expand Down Expand Up @@ -36,6 +37,8 @@ class ErrorBoundary extends Component {

componentDidCatch( error ) {
this.setState( { error } );

doAction( 'editor.ErrorBoundary.errorLogged', error );
}

reboot() {
Expand Down