From 61e38a443357afdec4516bfaa719d2116e49ae22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Javier=20Guzm=C3=A1n=20Jim=C3=A9nez?= <906599+javierguzman@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:48:04 +0200 Subject: [PATCH] feat: add error boundary (#534) --- src/renderer/ErrorBoundary.jsx | 29 +++++++++++++++++++++++++++++ src/renderer/index.tsx | 13 ++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/renderer/ErrorBoundary.jsx diff --git a/src/renderer/ErrorBoundary.jsx b/src/renderer/ErrorBoundary.jsx new file mode 100644 index 000000000..a9191cd75 --- /dev/null +++ b/src/renderer/ErrorBoundary.jsx @@ -0,0 +1,29 @@ +/* eslint-disable */ +import React from "react"; + +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { hasError: true, errorMessage: "" }; + } + + static getDerivedStateFromError(error) { + return { hasError: true, errorMessage: error.message }; + } + + render() { + if (this.state.hasError) { + return ( +
+

Uh oh!

+

This wasn't supposed to happen. If you continue to see this message, please reach out to support.

+

{this.state.errorMessage}

+
+ ); + } + + return this.props.children; + } +} + +export default ErrorBoundary; diff --git a/src/renderer/index.tsx b/src/renderer/index.tsx index a3c09cacc..8297b7a44 100644 --- a/src/renderer/index.tsx +++ b/src/renderer/index.tsx @@ -24,16 +24,19 @@ import Error from "./Error"; import "bootstrap-css-only/css/bootstrap.min.css"; import "@appigram/react-rangeslider/lib/index.css"; import i18n from "./i18n"; +import ErrorBoundary from "./ErrorBoundary"; const container = document.getElementById("root"); const root = createRoot(container); try { root.render( - - - - - , + + + + + + + , ); } catch (e) { root.render();