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

feat: add ErrorBoundary #69

Merged
merged 3 commits into from
Jan 15, 2025
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
41 changes: 41 additions & 0 deletions src/components/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { CircleAlert } from "lucide-react";
import { Header } from "./Header";
import { Card } from "./ui/card";

export function Error() {
return (
<div className="w-screen h-screen flex flex-col items-center justify-center">
<div className="flex-0 w-full">
<Header hasError />
</div>
<div className="h-24 flex flex-col flex-1 justify-center">
<Card className="p-8 flex flex-col items-center">
<CircleAlert className="text-red-600 mb-2 size-16" />
<div className="text-xl font-semibold text-gray-600 text-center">
An error occurred
</div>
<div className="text-md mb-4 text-gray-600 text-center text-balance">
If this issue persists, please reach out to us on{" "}
<a
className="underline text-gray-700"
href="https://discord.gg/stacklok"
rel="noopener noreferrer"
target="_blank"
>
Discord
</a>{" "}
or open a new{" "}
<a
className="underline text-gray-700"
href="https://github.com/stacklok/codegate/issues/new"
rel="noopener noreferrer"
target="_blank"
>
Github issue
</a>
</div>
</Card>
</div>
</div>
);
}
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 type { ReactNode } from "react";
import { Component } from "react";

interface Props {
children?: ReactNode;
fallback: ReactNode;
}

interface State {
hasError: boolean;
}

export default class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
};

public static getDerivedStateFromError(): State {
return { hasError: true };
}

public componentDidCatch() {
// this should log the error to a service like Sentry
}

public render() {
if (this.state.hasError) {
return this.props.fallback;
}

return this.props.children;
}
}
11 changes: 8 additions & 3 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Link } from "react-router-dom";
import { SidebarTrigger } from "./ui/sidebar";
import { Separator } from "./ui/separator";

export function Header() {
export function Header({ hasError }: { hasError?: boolean }) {
return (
<header className="flex-shrink-0 h-16 px-3 items-center flex w-full bg-teal-25 opacity-1 border-b-blue-200 border-b">
<div className="flex items-center flex-1">
<SidebarTrigger />
<Separator orientation="vertical" className="h-8 mx-3" />
{!hasError && (
<>
<SidebarTrigger />
<Separator orientation="vertical" className="h-8 mx-3" />
</>
)}

<nav className="mx-1 flex">
<Link to="/">
<h1 className="text-2xl w-max flex font-semibold">
Expand Down
22 changes: 22 additions & 0 deletions src/components/__tests__/ErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { render } from "@/lib/test-utils";
import { screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import ErrorBoundary from "../ErrorBoundary";
import { Error } from "../Error";

const ErrorComponent = () => {
throw Error();
};

describe("ErrorBoundary", () => {
it("renders fallback when a child throws an error", () => {
vi.spyOn(console, "error").mockImplementation(() => {});
render(
<ErrorBoundary fallback={<Error />}>
<ErrorComponent />
</ErrorBoundary>,
);

expect(screen.getByText(/an error occurred/i)).toBeVisible();
});
});
6 changes: 5 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import "./index.css";
import App from "./App.tsx";
import { BrowserRouter } from "react-router-dom";
import { SidebarProvider } from "./components/ui/sidebar.tsx";
import ErrorBoundary from "./components/ErrorBoundary.tsx";
import { Error } from "./components/Error.tsx";

createRoot(document.getElementById("root")!).render(
<StrictMode>
<BrowserRouter>
<SidebarProvider>
<App />
<ErrorBoundary fallback={<Error />}>
<App />
</ErrorBoundary>
</SidebarProvider>
</BrowserRouter>
</StrictMode>
Expand Down
Loading