Skip to content

Commit c396bfb

Browse files
committed
Add ErrorBoundary class and use it in models
1 parent 23895b4 commit c396bfb

2 files changed

Lines changed: 78 additions & 22 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from "react";
2+
import { Card, Typography } from "@equinor/eds-core-react";
3+
4+
type Props = {
5+
children: React.ReactNode;
6+
};
7+
8+
type State = {
9+
error?: Error;
10+
};
11+
12+
const Error: React.FC<{ error: Error }> = ({ error }) => (
13+
<Card variant="danger">
14+
<Card.Header>
15+
<Card.HeaderTitle>
16+
<Typography variant="h5">{error.name}</Typography>
17+
<Typography variant="body_short">{error.message}</Typography>
18+
</Card.HeaderTitle>
19+
</Card.Header>
20+
<Card.Content>
21+
<div style={{ overflow: "auto", fontFamily: "monospace" }}>
22+
<Typography variant="body_short" as="pre">
23+
{error.stack}
24+
</Typography>
25+
</div>
26+
</Card.Content>
27+
</Card>
28+
);
29+
30+
class ErrorBoundary extends React.Component<Props, State> {
31+
constructor(props: Props) {
32+
super(props);
33+
this.state = {
34+
error: undefined,
35+
};
36+
}
37+
38+
static getDerivedStateFromError(error: Error) {
39+
return { error };
40+
}
41+
42+
componentDidCatch(error: Error, info: React.ErrorInfo) {
43+
console.error(error, info.componentStack, React.captureOwnerStack());
44+
}
45+
46+
render() {
47+
const { error } = this.state;
48+
49+
return error !== undefined ? <Error error={error} /> : this.props.children;
50+
}
51+
}
52+
53+
export default ErrorBoundary;

frontend/src/pages/Models.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useGridRangeStore } from "@/hooks/useGridRangeStore";
2121
import InputStep from "@/components/Simulation/InputStep";
2222
import ResultStep from "@/components/Simulation/ResultStep";
2323
import GridResultStep from "@/components/GridSimulation/GridResultStep";
24+
import ErrorBoundary from "@/components/ErrorBoundary.tsx";
2425

2526
const Models: React.FC = () => {
2627
const [selectedModels, setSelectedModels] = useState<ModelConfig[]>([]);
@@ -166,29 +167,31 @@ const Models: React.FC = () => {
166167

167168
return (
168169
<MainContainer>
169-
<Step
170-
step={1}
171-
title="Models"
172-
description="Select models for simulation. Multiple models can be chained together in a pipeline."
173-
/>
174-
<ModelSelect selectedModels={selectedModels} setSelectedModels={setSelectedModels} />
175-
<Step step={2} title="Inputs" />
176-
<InputStep selectedModels={selectedModels} setModelInput={setModelInput} runGridSimulation={runGrid} />
177-
<Step step={3} title="Results" />
178-
{isGridMode ? (
179-
<GridResultStep
180-
result={gridResult}
181-
isLoading={gridIsLoading}
182-
error={gridResultError ?? startGridError}
170+
<ErrorBoundary>
171+
<Step
172+
step={1}
173+
title="Models"
174+
description="Select models for simulation. Multiple models can be chained together in a pipeline."
183175
/>
184-
) : (
185-
<ResultStep
186-
simulationResults={simulationResults}
187-
isLoading={simulationIsLoading}
188-
error={resultError ?? startError}
189-
/>
190-
)}
191-
<div style={{ height: "25dvh" }} />
176+
<ModelSelect selectedModels={selectedModels} setSelectedModels={setSelectedModels} />
177+
<Step step={2} title="Inputs" />
178+
<InputStep selectedModels={selectedModels} setModelInput={setModelInput} runGridSimulation={runGrid} />
179+
<Step step={3} title="Results" />
180+
{isGridMode ? (
181+
<GridResultStep
182+
result={gridResult}
183+
isLoading={gridIsLoading}
184+
error={gridResultError ?? startGridError}
185+
/>
186+
) : (
187+
<ResultStep
188+
simulationResults={simulationResults}
189+
isLoading={simulationIsLoading}
190+
error={resultError ?? startError}
191+
/>
192+
)}
193+
<div style={{ height: "25dvh" }} />
194+
</ErrorBoundary>
192195
</MainContainer>
193196
);
194197
};

0 commit comments

Comments
 (0)