From 5a79fdb6b8e1ed8d97a1edd327a3374cec1d018b Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 29 Dec 2017 11:45:26 +0000 Subject: [PATCH] Add an error boundary component to catch errors. --- src/components/ErrorBoundary.tsx | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/components/ErrorBoundary.tsx diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..e13ebe5 --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,37 @@ +import * as React from 'react'; + +class ErrorBoundary extends React.Component { + state: { + error?: Error; + }; + + constructor(props: any) { + super(props); + this.state = { }; + } + + componentDidCatch(error: Error, info: any) { + this.setState({ error }); + } + + render() { + if (this.state.error) { + return ( +
+

Something went wrong!

+
+            {this.state.error.message}
+          
+ +

Stack trace:

+
+            {this.state.error.stack}
+          
+
+ ); + } + return this.props.children; + } +} + +export default ErrorBoundary;