Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error boundaries WD-8089 #595

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,35 @@ import { InstanceLoadingProvider } from "context/instanceLoading";
import { ProjectProvider } from "context/project";
import Events from "pages/instances/Events";
import App from "./App";
import ErrorBoundary from "components/ErrorBoundary";
import ErrorPage from "components/ErrorPage";

const queryClient = new QueryClient();

const Root: FC = () => {
return (
<NotificationProvider>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ProjectProvider>
<InstanceLoadingProvider>
<EventQueueProvider>
<div className="l-application" role="presentation">
<Navigation />
<App />
<Panels />
<Events />
</div>
</EventQueueProvider>
</InstanceLoadingProvider>
</ProjectProvider>
</AuthProvider>
</QueryClientProvider>
</NotificationProvider>
<ErrorBoundary fallback={ErrorPage}>
<NotificationProvider>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ProjectProvider>
<InstanceLoadingProvider>
<EventQueueProvider>
<div className="l-application" role="presentation">
<Navigation />
<ErrorBoundary fallback={ErrorPage}>
<App />
<Panels />
<Events />
</ErrorBoundary>
</div>
</EventQueueProvider>
</InstanceLoadingProvider>
</ProjectProvider>
</AuthProvider>
</QueryClientProvider>
</NotificationProvider>
</ErrorBoundary>
);
};

Expand Down
33 changes: 33 additions & 0 deletions src/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { FC } from "react";
import type { PropsWithChildren } from "react";
import { Component } from "react";

type Props = PropsWithChildren & {
fallback: FC<{ error?: Error }>;
};

type State = {
error?: Error;
hasError: boolean;
};

export default class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false };
}

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

render() {
const { error, hasError } = this.state;
const { children, fallback: ErrorComponent } = this.props;

return hasError ? <ErrorComponent error={error} /> : <>{children}</>;
}
}
55 changes: 55 additions & 0 deletions src/components/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { FC } from "react";
import {
CodeSnippet,
CodeSnippetBlockAppearance,
Notification,
Strip,
} from "@canonical/react-components";

type Props = {
error?: Error;
};

const ErrorPage: FC<Props> = ({ error }) => {
const body = encodeURIComponent(
`\`\`\`\n${error?.stack ?? "No stack track"}\n\`\`\``,
);
const url = `https://github.com/canonical/lxd-ui/issues/new?labels=bug&title=Error%20report&body=${body}`;

return (
<Strip>
<Notification severity="negative" title="Error">
Something has gone wrong. If this issue persists,{" "}
<a href={url} rel="noopener noreferrer" target="_blank">
please raise an issue on GitHub.
</a>
</Notification>
<CodeSnippet
blocks={[
...(error?.message
? [
{
title: "Error",
appearance: CodeSnippetBlockAppearance.NUMBERED,
wrapLines: true,
code: error.message,
},
]
: []),
...(error?.stack
? [
{
title: "Stack trace",
appearance: CodeSnippetBlockAppearance.NUMBERED,
wrapLines: true,
code: error.stack,
},
]
: []),
]}
/>
</Strip>
);
};

export default ErrorPage;
Loading