From d85778de38447f5f9b947fb83080c2bb75425ef9 Mon Sep 17 00:00:00 2001 From: pecatoma Date: Wed, 17 Mar 2021 21:14:05 +0100 Subject: [PATCH 1/2] Handle error on higher tree level --- src/app.tsx | 50 +++++++------------ .../app-error-boundary.component.tsx | 27 ++++++++++ src/index.tsx | 25 ++++++---- 3 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/components/app-error-boundary/app-error-boundary.component.tsx diff --git a/src/app.tsx b/src/app.tsx index 742d1a81d..5f6f8d6a9 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -24,8 +24,6 @@ import ScssVars from "./assets/styles/styles/custom/_variables.module.scss"; import { ApplicationStateModel } from "./state/models/application-state.model"; import { ScheduleKey } from "./api/persistance-store.model"; import { AppMode, useAppConfig } from "./state/app-config-context"; -import * as Sentry from "@sentry/react"; -import AppErrorModal from "./components/common-components/modal/app-error-modal/app-error.modal.component"; import { cropScheduleDMToMonthDM } from "./logic/schedule-container-convertion/schedule-container-convertion"; const useStyles = makeStyles(() => ({ @@ -104,39 +102,25 @@ function App(): JSX.Element { fetchGlobalState(); }, [fetchGlobalState]); - const [open, setIsOpen] = useState(false); - const fallback = useCallback( - ({ resetError }): JSX.Element => ( - - ), - [open, setIsOpen] - ); - - const onError = useCallback((): void => { - setIsOpen(true); - }, [setIsOpen]); - return ( - - - - - - - - - - - - - + + + + + + + + + + + - {isElectron() ? <> : } - - - - - + + {isElectron() ? <> : } + + + + ); } diff --git a/src/components/app-error-boundary/app-error-boundary.component.tsx b/src/components/app-error-boundary/app-error-boundary.component.tsx new file mode 100644 index 000000000..337bbd24e --- /dev/null +++ b/src/components/app-error-boundary/app-error-boundary.component.tsx @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +import React, { ReactNode, useCallback, useState } from "react"; +import AppErrorModal from "../common-components/modal/app-error-modal/app-error.modal.component"; +import * as Sentry from "@sentry/react"; + +interface AppErrorBoundaryOptions { + children: ReactNode; +} + +export function AppErrorBoundary(props: AppErrorBoundaryOptions): JSX.Element { + const [open, setIsOpen] = useState(false); + const fallback = useCallback( + ({ resetError }): JSX.Element => ( + + ), + [open, setIsOpen] + ); + + const onError = useCallback((): void => { + setIsOpen(true); + }, [setIsOpen]); + + return ; +} diff --git a/src/index.tsx b/src/index.tsx index ebce7229a..14cd81df1 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -19,6 +19,7 @@ import thunkMiddleware from "redux-thunk"; import { appReducer } from "./state/app.reducer"; import { createBrowserHistory } from "history"; import { composeWithDevTools } from "redux-devtools-extension"; +import { AppErrorBoundary } from "./components/app-error-boundary/app-error-boundary.component"; const history = createBrowserHistory(); @@ -42,17 +43,19 @@ const composedEnhancer = composeWithDevTools( export const appStore = createStore(appReducer, composedEnhancer); ReactDOM.render( - - - - - - - - - - - , + + + + + + + + + + + + + , document.getElementById("root") ); /* eslint-disable @typescript-eslint/no-explicit-any */ From dd20fe6407d41ceb356a0e7a2232cd871024d216 Mon Sep 17 00:00:00 2001 From: pecatoma Date: Wed, 17 Mar 2021 21:26:29 +0100 Subject: [PATCH 2/2] Add error message for testing purposes --- .../report-issue-modal/report-issue-modal.component.tsx | 8 ++++++++ .../schedule-page/table/schedule/schedule.component.tsx | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/common-components/modal/report-issue-modal/report-issue-modal.component.tsx b/src/components/common-components/modal/report-issue-modal/report-issue-modal.component.tsx index 3017b7889..a02cee7a4 100644 --- a/src/components/common-components/modal/report-issue-modal/report-issue-modal.component.tsx +++ b/src/components/common-components/modal/report-issue-modal/report-issue-modal.component.tsx @@ -117,6 +117,14 @@ export default function ReportIssueModal(options: ReportIssueModalOptions): JSX. }); } + // Only for testing purposes + if ( + process.env.REACT_APP_TEST_MODE && + issueDescription.toLowerCase() === process.env.REACT_APP_ERROR_WORKER + ) { + throw new Error("[TEST MODE] Error message was entered"); + } + const body = (
{isSent &&

Wysłano powiadomienie o błędzie.

} diff --git a/src/components/schedule-page/table/schedule/schedule.component.tsx b/src/components/schedule-page/table/schedule/schedule.component.tsx index 7a990d7c3..7da457164 100644 --- a/src/components/schedule-page/table/schedule/schedule.component.tsx +++ b/src/components/schedule-page/table/schedule/schedule.component.tsx @@ -35,7 +35,7 @@ export function ScheduleComponent({ .map((worker) => worker.toLowerCase()) .includes(process.env.REACT_APP_ERROR_WORKER ?? "ERROR") ) { - throw new Error("Schedule in dev mode includes error user"); + throw new Error("[TEST MODE] Error user was added"); } return (