Skip to content
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
26 changes: 26 additions & 0 deletions apps/timo-web/components/boundary/AsyncBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Suspense, type ReactNode } from "react";

import {
ErrorBoundary,
type ErrorFallbackTypes,
} from "@/components/boundary/ErrorBoundary";

interface AsyncBoundaryProps {
children: ReactNode;
pendingFallback?: ReactNode;
errorFallback?: ErrorFallbackTypes;
}

export const AsyncBoundary = ({
children,
pendingFallback = null,
errorFallback,
}: AsyncBoundaryProps) => {
const content = <Suspense fallback={pendingFallback}>{children}</Suspense>;

if (!errorFallback) {
return content;
}

return <ErrorBoundary fallback={errorFallback}>{content}</ErrorBoundary>;
};
56 changes: 56 additions & 0 deletions apps/timo-web/components/boundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use client";

import * as Sentry from "@sentry/nextjs";
import { Component } from "react";

import type { ErrorInfo, ReactNode } from "react";

export type ErrorFallbackTypes =
| ReactNode
| ((error: Error, reset: () => void) => ReactNode);

interface ErrorBoundaryProps {
children: ReactNode;
fallback: ErrorFallbackTypes;
}

interface ErrorBoundaryState {
error: Error | null;
}
Comment thread
kimminna marked this conversation as resolved.

const toError = (value: unknown): Error =>
value instanceof Error ? value : new Error(String(value));

export class ErrorBoundary extends Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
state: ErrorBoundaryState = { error: null };

static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
return { error: toError(error) };
}

componentDidCatch(error: unknown, errorInfo: ErrorInfo): void {
Sentry.captureException(toError(error), {
contexts: { react: { componentStack: errorInfo.componentStack } },
});
}

reset = (): void => {
this.setState({ error: null });
};

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

if (error) {
return typeof fallback === "function"
? fallback(error, this.reset)
: fallback;
}

return children;
}
}
Comment thread
kimminna marked this conversation as resolved.
Loading