redoc/src/components/ErrorBoundary.tsx

41 lines
988 B
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import * as React from 'react';
import { Children } from 'react';
import styled from '../styled-components';
const ErrorWrapper = styled.div`
padding: 20px;
color: red;
`;
export class ErrorBoundary extends React.Component<{}, { error?: Error }> {
constructor(props) {
super(props);
this.state = { error: undefined };
}
componentDidCatch(error) {
this.setState({ error: error });
return false;
}
render() {
if (this.state.error) {
return (
<ErrorWrapper>
<h1>Something went wrong...</h1>
2017-10-12 00:01:37 +03:00
<small> {this.state.error.message} </small>
<p>
<details>
<summary>Stack trace</summary>
<pre>{this.state.error.stack}</pre>
</details>
</p>
<small> ReDoc Version: {__REDOC_VERSION__}</small> <br />
<small> Commit: {__REDOC_REVISION__}</small>
2017-10-12 00:01:37 +03:00
</ErrorWrapper>
);
}
return Children.only(this.props.children);
}
}