1
0
mirror of https://github.com/SomboChea/ui synced 2024-11-11 00:54:26 +07:00
verdaccio-ui/src/App/AppError.tsx

39 lines
867 B
TypeScript
Raw Normal View History

import React, { Component } from 'react';
export interface ErrorProps {
children: any;
}
export interface ErrorAppState {
hasError: boolean;
2019-10-11 03:20:05 +07:00
error: Error | null;
info: object | null;
}
export default class ErrorBoundary extends Component<ErrorProps, ErrorAppState> {
2019-10-11 03:20:05 +07:00
constructor(props: ErrorProps) {
super(props);
this.state = { hasError: false, error: null, info: null };
}
2019-10-11 03:20:05 +07:00
public componentDidCatch(error: Error, info: object) {
this.setState({ hasError: true, error, info });
}
2019-10-03 02:52:27 +07:00
public render(): JSX.Element {
const { hasError, error, info } = this.state;
const { children } = this.props;
if (hasError) {
return (
<>
<h1>{'Something went wrong.'}</h1>
<p>{`error: ${error}`}</p>
<p>{`info: ${JSON.stringify(info)}`}</p>
</>
);
}
return children;
}
}