// Copyright (c) 2025-2026, RTE (https://www.rte-france.com) // This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. // If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, // you can obtain one at http://mozilla.org/MPL/2.0/. // SPDX-License-Identifier: MPL-2.0 // This file is part of Co-Study4Grid a Power Grid Study tool Assistant Interface to help solve contigencies for a grid state under study. import { Component, type ErrorInfo, type ReactNode } from 'react'; import { colors } from '../styles/tokens'; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } /** * React Error Boundary that catches unhandled exceptions in the component * tree below it and renders a fallback UI instead of crashing the whole app * with a white screen. * * Usage: wrap the app root (or any subtree you want to isolate) in * .... Optionally pass a `fallback` prop to * override the default fallback UI. */ export class ErrorBoundary extends Component { state: ErrorBoundaryState = { hasError: false, error: null, errorInfo: null, }; static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo): void { // Log to console so developers can find the stack trace in the // browser devtools. In production this could also ship to a // telemetry backend. console.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ errorInfo }); } handleReset = (): void => { this.setState({ hasError: false, error: null, errorInfo: null }); }; handleReload = (): void => { window.location.reload(); }; render(): ReactNode { if (this.state.hasError) { if (this.props.fallback !== undefined) { return this.props.fallback; } const { error, errorInfo } = this.state; return (

Something went wrong

An unexpected error occurred in the application. You can try recovering without losing your session, or reload the page if the error persists.

{error && (
Error details
{error.name}: {error.message}
{error.stack && (
                                    {error.stack}
                                
)} {errorInfo?.componentStack && (
                                    {errorInfo.componentStack}
                                
)}
)}
); } return this.props.children; } } export default ErrorBoundary;