-
Notifications
You must be signed in to change notification settings - Fork 172
/
with-error-boundary.tsx
75 lines (65 loc) · 1.85 KB
/
with-error-boundary.tsx
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
/*
* Wazuh app - React HOCs to manage user authorization requirements
* Copyright (C) 2015-2021 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import React, { Component } from 'react';
import log from 'loglevel';
import { EuiEmptyPrompt } from '@elastic/eui';
export default class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch = (error, errorInfo) => catchFunc(error, errorInfo, this);
render() {
// @ts-ignore
if (this.state.errorInfo) {
return <HandleError props={this} />;
}
return this.props.children;
}
}
export const withErrorBoundary = (WrappedComponent) => (props) => (
<ErrorBoundary>
<WrappedComponent {...props} />
</ErrorBoundary>
);
const catchFunc = (error, errorInfo, ctx) => {
ctx.setState({
error: error,
errorInfo: errorInfo,
});
log.error({ error, errorInfo });
};
const ErrorComponent = (props: { ctx: any }) => {
const styles = {
error: {
borderTop: '1px solid #777',
borderBottom: '1px solid #777',
padding: '12px',
},
};
return (
<div style={props.ctx.props.style || styles.error}>
<details style={{ whiteSpace: 'pre-wrap' }}>
{props.ctx.props.state.error && props.ctx.props.state.error.toString()}
<br />
{props.ctx.props.state.errorInfo.componentStack}
</details>
</div>
);
};
const HandleError = (ctx) => (
<EuiEmptyPrompt
iconType="faceSad"
title={<h2>Something went wrong.</h2>}
body={<ErrorComponent ctx={ctx} />}
/>
);