forked from wso2/carbon-apimgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppErrorBoundary.jsx
185 lines (179 loc) · 6.4 KB
/
AppErrorBoundary.jsx
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { Toolbar, AppBar } from '@material-ui/core';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import PropTypes from 'prop-types';
import Footer from 'AppComponents/Base/Footer/Footer';
const styles = theme => ({
appBar: {
zIndex: theme.zIndex.modal + 1,
position: 'relative',
background: theme.palette.background.appBar,
},
typoRoot: {
marginLeft: theme.spacing.unit * 3,
marginRight: theme.spacing.unit * 3,
textTransform: 'capitalize',
},
brandLink: {
color: theme.palette.primary.contrastText,
},
toolbar: {
minHeight: 56,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
minHeight: 48,
},
[theme.breakpoints.up('sm')]: {
minHeight: 64,
},
},
menuIcon: {
color: theme.palette.getContrastText(theme.palette.background.appBar),
fontSize: 35,
},
errorDisplay: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
},
errorDisplayContent: {
width: 960,
display: 'flex',
flexDirection: 'column',
overflow: 'auto',
},
errorTitle: {
display: 'flex',
alignItems: 'center',
paddingTop: theme.spacing.unit * 2,
paddingBottom: theme.spacing.unit * 2,
'& h2': {
paddingLeft: theme.spacing.unit * 2,
},
},
link: {
color: theme.palette.getContrastText(theme.palette.background.default),
},
});
/**
* Error boundary for the application.catch JavaScript errors anywhere in their child component tree,
* log those errors, and display a fallback UI instead of the component tree that crashed.
* Error boundaries catch errors during rendering, in lifecycle methods,
* and in constructors of the whole tree below them.
* @class AppErrorBoundary
* @extends {Component}
*/
class AppErrorBoundary extends React.Component {
/**
* Creates an instance of AppErrorBoundary.
* @param {any} props @inheritDoc
* @memberof AppErrorBoundary
*/
constructor(props) {
super(props);
this.state = {
hasError: false,
};
}
/**
* The componentDidCatch() method works like a JavaScript catch {} block, but for components.
* @param {Error} error is an error that has been thrown
* @param {Object} info info is an object with componentStack key. The property has information about component
* stack during thrown error.
* @memberof AppErrorBoundary
*/
componentDidCatch(error, info) {
this.setState({ hasError: true, error, info });
}
/**
* Return error handled UI
* @returns {React.Component} return react component
* @memberof AppErrorBoundary
*/
render() {
const { hasError, error, info } = this.state;
const { children, classes, theme } = this.props;
const errorStackStyle = {
background: '#fff8dc',
};
if (hasError) {
return (
<React.Fragment>
<AppBar className={classes.appBar} position='fixed'>
<Toolbar className={classes.toolbar}>
<div className={classes.errorDisplay} style={{ width: '100%' }}>
<div className={classes.errorDisplayContent}>
<a href='/publisher-new'>
<img src={theme.custom.logo} alt={theme.custom.title} />
</a>
</div>
</div>
</Toolbar>
</AppBar>
<div className={classes.errorDisplay}>
<div className={classes.errorDisplayContent}>
<div className={classes.errorTitle}>
<img src='/publisher-new/site/public/images/robo.png' alt='OOPS' />
<Typography variant='h2' gutterBottom>
Something went wrong
</Typography>
</div>
<a href='/publisher-new/apis/'>
<h3 className={classes.link}>API Listing</h3>
</a>
</div>
</div>
<div className={classes.errorDisplay}>
<div className={classes.errorDisplayContent}>
<h3 style={{ color: 'red' }}>{error.message}</h3>
<pre style={errorStackStyle}>
<u>{error.stack}</u>
</pre>
<pre style={errorStackStyle}>
<u>{info.componentStack}</u>
</pre>
</div>
</div>
<Footer />
</React.Fragment>
);
} else {
return children;
}
}
}
AppErrorBoundary.propTypes = {
children: PropTypes.node.isRequired,
classes: PropTypes.shape({
appBar: PropTypes.string,
toolbar: PropTypes.string,
errorDisplay: PropTypes.string,
errorDisplayContent: PropTypes.string,
errorTitle: PropTypes.string,
link: PropTypes.string,
}).isRequired,
theme: PropTypes.shape({
custom: PropTypes.shape({
logo: PropTypes.string,
title: PropTypes.string,
}),
}).isRequired,
};
export default withStyles(styles, { withTheme: true })(AppErrorBoundary);