From 1067f4d22fb998a02144eaaecc2151e48d01c9e6 Mon Sep 17 00:00:00 2001 From: sumeetpardeshi Date: Thu, 8 May 2025 17:01:18 -0700 Subject: [PATCH] handling duplication in catch block as per PR comments --- client/src/components/Sidebar.tsx | 33 ++++++++++++------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/client/src/components/Sidebar.tsx b/client/src/components/Sidebar.tsx index 180e8a2..5efa721 100644 --- a/client/src/components/Sidebar.tsx +++ b/client/src/components/Sidebar.tsx @@ -102,6 +102,15 @@ const Sidebar = ({ const [copiedServerFile, setCopiedServerFile] = useState(false); const { toast } = useToast(); + // Reusable error reporter for copy actions + function reportError(error: unknown) { + toast({ + title: "Error", + description: `Failed to copy config: ${error instanceof Error ? error.message : String(error)}`, + variant: "destructive", + }); + } + // Shared utility function to generate server config const generateServerConfig = useCallback(() => { if (transportType === "stdio") { @@ -159,18 +168,10 @@ const Sidebar = ({ }, 2000); }) .catch((error) => { - toast({ - title: "Error", - description: `Failed to copy config: ${error instanceof Error ? error.message : String(error)}`, - variant: "destructive", - }); + reportError(error); }); } catch (error) { - toast({ - title: "Error", - description: `Failed to copy config: ${error instanceof Error ? error.message : String(error)}`, - variant: "destructive", - }); + reportError(error); } }, [generateMCPServerEntry, transportType, toast]); @@ -193,18 +194,10 @@ const Sidebar = ({ }, 2000); }) .catch((error) => { - toast({ - title: "Error", - description: `Failed to copy config: ${error instanceof Error ? error.message : String(error)}`, - variant: "destructive", - }); + reportError(error); }); } catch (error) { - toast({ - title: "Error", - description: `Failed to copy config: ${error instanceof Error ? error.message : String(error)}`, - variant: "destructive", - }); + reportError(error); } }, [generateMCPServerFile, toast]);