From 081a46638bd6621fe7d5b6bfa7751319dba91d34 Mon Sep 17 00:00:00 2001 From: Marko Djordjevic Date: Wed, 18 Feb 2026 20:26:57 +0100 Subject: [PATCH] code-review-fix task 10.1: create ErrorBoundary component with fallback UI --- src/components/ErrorBoundary.tsx | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/components/ErrorBoundary.tsx diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..5bbce6b --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,66 @@ +"use client"; + +import React, { Component, ErrorInfo, ReactNode } from "react"; +import { Button } from "@/components/ui/button"; + +interface Props { + children: ReactNode; +} + +interface State { + hasError: boolean; + error: Error | null; +} + +class ErrorBoundary extends Component { + constructor(props: Props) { + super(props); + this.state = { hasError: false, error: null }; + } + + static getDerivedStateFromError(error: Error): State { + return { hasError: true, error }; + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo): void { + console.error("ErrorBoundary caught an error:", error, errorInfo); + } + + handleTryAgain = (): void => { + this.setState({ hasError: false, error: null }); + }; + + handleReload = (): void => { + window.location.reload(); + }; + + render(): ReactNode { + if (this.state.hasError) { + return ( +
+
+

+ Something went wrong +

+ {this.state.error && ( +

+ {this.state.error.message} +

+ )} +
+
+ + +
+
+ ); + } + + return this.props.children; + } +} + +export { ErrorBoundary }; +export default ErrorBoundary;