redoc/src/components/ErrorBoundary.tsx

43 lines
989 B
TypeScript
Raw Normal View History

2017-10-12 00:01:37 +03:00
import * as React from 'react';
import styled from '../styled-components';
const ErrorWrapper = styled.div`
padding: 20px;
color: red;
`;
2020-07-25 20:14:11 +03:00
export class ErrorBoundary extends React.Component<
React.PropsWithChildren<unknown>,
{ error?: Error }
> {
2017-10-12 00:01:37 +03:00
constructor(props) {
super(props);
this.state = { error: undefined };
}
componentDidCatch(error) {
2018-01-22 21:30:53 +03:00
this.setState({ error });
2017-10-12 00:01:37 +03:00
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>
);
}
2018-01-22 21:30:53 +03:00
return React.Children.only(this.props.children);
2017-10-12 00:01:37 +03:00
}
}