-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
76 lines (64 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button, ClipboardButton } from '@wordpress/components';
/**
* Internal dependencies
*/
import { Warning } from '../';
import { getEditedPostContent } from '../../store/selectors';
class ErrorBoundary extends Component {
constructor() {
super( ...arguments );
this.reboot = this.reboot.bind( this );
this.getContent = this.getContent.bind( this );
this.state = {
error: null,
};
}
componentDidCatch( error ) {
this.setState( { error } );
}
reboot() {
this.props.onError();
}
getContent() {
try {
return getEditedPostContent( this.context.store.getState() );
} catch ( error ) {}
}
render() {
const { error } = this.state;
if ( ! error ) {
return this.props.children;
}
return (
<Warning
className="editor-error-boundary"
actions={ [
<Button key="recovery" onClick={ this.reboot } isLarge>
{ __( 'Attempt Recovery' ) }
</Button>,
<ClipboardButton key="copy-post" text={ this.getContent } isLarge>
{ __( 'Copy Post Text' ) }
</ClipboardButton>,
<ClipboardButton key="copy-error" text={ error.stack } isLarge>
{ __( 'Copy Error' ) }
</ClipboardButton>,
] }
>
{ __( 'The editor has encountered an unexpected error.' ) }
</Warning>
);
}
}
ErrorBoundary.contextTypes = {
store: noop,
};
export default ErrorBoundary;