From a76f6426c42965eb9925390eb584cab9a070ae7b Mon Sep 17 00:00:00 2001 From: "hyunwoo93.cha" Date: Mon, 12 Aug 2024 10:33:15 +0900 Subject: [PATCH] add errorboundary --- com.twin.app.shoptime/src/App/App.js | 19 ++++++---- .../src/views/ErrorBoundary.js | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 com.twin.app.shoptime/src/views/ErrorBoundary.js diff --git a/com.twin.app.shoptime/src/App/App.js b/com.twin.app.shoptime/src/App/App.js index a0aa3272..6185a4ba 100644 --- a/com.twin.app.shoptime/src/App/App.js +++ b/com.twin.app.shoptime/src/App/App.js @@ -38,6 +38,7 @@ import { checkValidCountry } from "../lunaSend/common"; import { lunaTest } from "../lunaSend/lunaTest"; import * as Config from "../utils/Config"; import { $L, clearLaunchParams, getLaunchParams } from "../utils/helperMethods"; +import ErrorBoundary from "../views/ErrorBoundary"; import MainView from "../views/MainView/MainView"; import css from "./App.module.less"; import { getMenuByLinkTpCd, handleDeepLink } from "./deepLinkHandler"; @@ -326,14 +327,16 @@ function AppBase(props) { }, [dispatch]); return ( - + + + ); } diff --git a/com.twin.app.shoptime/src/views/ErrorBoundary.js b/com.twin.app.shoptime/src/views/ErrorBoundary.js new file mode 100644 index 00000000..1af4f8e6 --- /dev/null +++ b/com.twin.app.shoptime/src/views/ErrorBoundary.js @@ -0,0 +1,38 @@ +import React, { Component } from "react"; + +import { connect } from "react-redux"; + +import { clearLaunchParams } from "../utils/helperMethods"; + +class ErrorBoundary extends Component { + constructor(props) { + super(props); + this.state = { hasError: false }; + } + + static getDerivedStateFromError(error) { + return { hasError: true }; + } + + componentDidCatch(error, errorInfo) { + console.error("Uncaught error:", error, errorInfo); + } + componentDidUpdate(prevProps, prevState) { + if (this.state.hasError) { + clearLaunchParams(); + if (typeof window === "object") { + window.location.reload(); + } + } + } + + render() { + if (this.state.hasError) { + return
Something went wrong.
; + } + + return this.props.children; + } +} + +export default connect()(ErrorBoundary);