/* * Copyright (c) 2015 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause */ import React, { type ReactNode } from 'react'; import { getCurrentWindow } from '@electron/remote'; import Button from '../Button/Button'; import { type Device } from '../Device/deviceSlice'; import FactoryResetButton from '../FactoryReset/FactoryResetButton'; import { Group } from '../Group/Group'; import Spinner from '../Spinner/Spinner'; import telemetry from '../telemetry/telemetry'; import { openUrl } from '../utils/open'; import { packageJson } from '../utils/packageJson'; import { getAppSpecificStore as store } from '../utils/persistentStore'; import { generateSystemReport } from '../utils/systemReport'; import bugIcon from './bug.svg'; import './error-boundary.scss'; const sendErrorReport = (error: string) => { telemetry.sendErrorReport(error); }; interface Props { children: ReactNode; selectedSerialNumber?: string; selectedDevice?: Device; devices?: Device[]; appName?: string; restoreDefaults?: () => void; sendTelemetryEvent?: (message: string) => void; } const genericRestoreDefaults = () => { store().clear(); getCurrentWindow().reload(); }; class ErrorBoundary extends React.Component< Props, { hasError: boolean; error: Error | null; systemReport: string | null } > { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, systemReport: null, }; } static getDerivedStateFromError(error: Error) { return { hasError: true, error, }; } componentDidCatch(error: Error) { const { devices, selectedDevice, selectedSerialNumber, sendTelemetryEvent, } = this.props; if (sendTelemetryEvent != null) { sendTelemetryEvent(error.message); } else { sendErrorReport(error.message); } generateSystemReport( new Date().toISOString().replace(/:/g, '-'), devices, selectedDevice, selectedSerialNumber, ).then(report => { this.setState({ systemReport: report }); }); } render() { const { hasError, error, systemReport } = this.state; const { children, appName, restoreDefaults } = this.props; if (!hasError) { return children; } const appDisplayName = appName || packageJson().displayName || packageJson().name; return (

Oops! There was a problem

nRF Connect for Desktop {appDisplayName} experienced an unrecoverable error

If this is the first time you've seen this problem we recommend restarting the application.

If restarting didn't help, you can also try restoring to default values.

{error !== null && (

{error.message}

{error.stack}
)}
{systemReport !== null ? (
{systemReport}
) : (

Generating system report...

)}

Please report the problem on DevZone if you have experienced it multiple times

); } } export default ErrorBoundary;