chore: update error components for loading file failed

This commit is contained in:
jialin
2025-05-15 22:57:24 +08:00
parent 17ec70f839
commit 00a60b8948
8 changed files with 83 additions and 29 deletions
+16 -22
View File
@@ -1,35 +1,29 @@
import { Component } from 'react';
import type { ErrorInfo } from 'react';
import React from 'react';
import ErrorResult from './error-result';
interface ErrorBoundaryState {
hasError: boolean;
}
class ErrorBoundary extends Component<
// eslint-disable-next-line @typescript-eslint/ban-types
class ErrorBoundary extends React.Component<
{ children?: React.ReactNode },
ErrorBoundaryState
{ hasError: boolean; errorInfo: string }
> {
constructor(props: any) {
super(props);
this.state = {
hasError: false
};
state = { hasError: false, errorInfo: '' };
static getDerivedStateFromError(error: Error) {
return { hasError: true, errorInfo: error.message };
}
static getDerivedStateFromError(error: any) {
console.error('Error caught by Error Boundary:', error);
return { hasError: true };
}
componentDidCatch(error: any, info: any) {
console.error('Error caught by Error Boundary:', error);
console.error(info);
componentDidCatch(error: any, errorInfo: ErrorInfo) {
// You can also log the error to an error reporting service
// eslint-disable-next-line no-console
console.log(error, errorInfo);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
// You can render any custom fallback UI
return <ErrorResult extra={this.state.errorInfo} />;
}
return this.props.children;
}
}