Skip to content

Commit

Permalink
fix: error-boundary in page assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
yb6b committed Jan 27, 2025
1 parent c40bcd0 commit af63864
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
46 changes: 45 additions & 1 deletion src/components/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import { Result } from "antd";
import ConfigManager from "./ConfigManager";
import React from "react";
import { useRouteError } from "react-router-dom";

export default function ErrorResult() {
function BacsErrorResult() {
return (
<Result
status="500"
Expand All @@ -15,3 +17,45 @@ export default function ErrorResult() {
/>
);
}

export default function ErrorResult(props: any) {
const error = useRouteError() as any;
if (error || props.error) {
return (
<>
<p className="text-center">
捕获到错误:
<span className="text-red-500">
{" "}
{error?.message || props.error.message}
</span>
</p>
<BacsErrorResult />
</>
);
}
return <BacsErrorResult />;
}

export class ErrorBoundary extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = { hasError: false, error: null };
}

static getDerivedStateFromError(error: any) {
return { hasError: true };
}

componentDidCatch(error: any, errorInfo: any) {
this.setState({ error });
console.error(error, errorInfo);
}

render() {
if (this.state.hasError) {
return <ErrorResult {...this.state} />;
}
return this.props.children;
}
}
11 changes: 7 additions & 4 deletions src/pages/[id]/assembly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ShortCodeRules from "~/components/ShortCodeRules";
import { useChaifenTitle } from "~/atoms";
import SelectRules from "~/components/SelectRules";
import MetricTable from "~/components/MetricTable";
import { ErrorBoundary } from "~/components/Error";

const ConfigureRules = () => {
return (
Expand All @@ -24,10 +25,12 @@ export default function Assembly() {
return (
<Flex vertical gap="middle">
<ConfigureRules />
<Suspense fallback={<Skeleton active />}>
<MetricTable />
<SequenceTable />
</Suspense>
<ErrorBoundary>
<Suspense fallback={<Skeleton active />}>
<MetricTable />
<SequenceTable />
</Suspense>
</ErrorBoundary>
</Flex>
);
}

0 comments on commit af63864

Please sign in to comment.