diff --git a/components/workspace/use-cli-commands.tsx b/components/workspace/use-cli-commands.tsx index de51adb..a3f578a 100644 --- a/components/workspace/use-cli-commands.tsx +++ b/components/workspace/use-cli-commands.tsx @@ -22,6 +22,7 @@ import { Link, useSearchParams } from "react-router-dom"; import { FormatBuildErrors } from "./format-build-errors"; import { playgroundService } from "@/data/playground-service"; import { solangBuildService } from "@/data/solang-build-service"; +import { useToast } from "../ui/use-toast"; const PROPOSAL_TIMEOUT = 15 * 60 * 1000; // proposal expires after 15 minutes @@ -30,6 +31,7 @@ export function useCliCommands() { const pathname = usePathname(); const id = useWorkspaceId(); const [, setSearchParams] = useSearchParams(); + const { toast } = useToast(); const wallet = useWallet(); const commands = { @@ -110,9 +112,26 @@ export function useCliCommands() { ); } catch (err) { - if (err instanceof Error) - terminalContext.setBufferedContent(

Error: {err.message}

); - return; + console.log("err",err) + const errorMessage = err instanceof Error ? err.message : "An unknown error occurred."; + terminalContext.setBufferedContent( +

+ Error: {errorMessage}. Please try again or contact + support if this continues. +

+ ); + if (errorMessage.includes("network")) { + toast({ + title: "Network Error", + description: "Please check your internet connection and try again.", + }); + } else { + toast({ + title: "Audit Failed", + description: + errorMessage || "Something went wrong. Please try again.", + }); + } } }, build: async () => { @@ -160,10 +179,18 @@ export function useCliCommands() { ); return; + } else { + throw new Error("Build failed: DLL generation returned undefined."); } } catch (err) { - if (err instanceof Error) - terminalContext.setBufferedContent(); + const errorMessage = err instanceof Error ? err.message : "An unknown error occurred."; + terminalContext.setBufferedContent( + + ); + toast({ + title: "Build Failed", + description: errorMessage, + }); return; } }, @@ -199,12 +226,23 @@ export function useCliCommands() { ); } catch (err) { - if (err instanceof Error) - terminalContext.setBufferedContent(<>{err.message}); + const errorMessage = err instanceof Error ? err.message : "An unknown error occurred."; + // Display error in the terminal + terminalContext.setBufferedContent( + <> +

Error: {errorMessage}

+ + ); + + toast({ + title: "Test Failed", + description: errorMessage, + }); return; } }, deploy: async () => { + try { if (typeof id !== "string") { terminalContext.setBufferedContent( <> @@ -217,7 +255,8 @@ export function useCliCommands() { if (!dll) { terminalContext.setBufferedContent( <> -

Contract not built.

+ Warning: + No contract found to deploy. Please ensure the contract is built successfully before attempting deployment. ); return; @@ -243,6 +282,18 @@ export function useCliCommands() { ); + } catch (err) { + const errorMessage = err instanceof Error ? err.message : "An unknown error occurred during deployment."; + terminalContext.setBufferedContent( + <> +

Error: {errorMessage}

+ + ); + toast({ + title: "Deployment Failed", + description: errorMessage, + }); + } }, check: async (id: string) => { if (!id) return `Please enter the Transaction ID.`; @@ -280,41 +331,76 @@ export function useCliCommands() { saveAs(file); }, share: async () => { - if (typeof id !== "string") throw new Error("id is not string"); - const start = `${pathname}/`; - terminalContext.setBufferedContent( - <> -

Loading files...

- - ); - const files = ( - await db.files.filter((file) => file.path.startsWith(start)).toArray() - ).map((file) => ({ - path: decodeURIComponent(file.path.replace(start, "")), - contents: file.contents, - })); + try { + // Validate `id` input + if (typeof id !== "string") { + terminalContext.setBufferedContent( + <> +

+ Error: Invalid workspace identifier. Please provide a valid + workspace ID. +

+ + ); + return; + } - terminalContext.setBufferedContent( - <> -

Loaded files: {files.map((i) => i.path).join(", ")}

-

- Generating share - link... -

- - ); + const start = `${pathname}/`; + terminalContext.setBufferedContent( + <> +

Loading files...

+ + ); + const files = ( + await db.files.filter((file) => file.path.startsWith(start)).toArray() + ).map((file) => ({ + path: decodeURIComponent(file.path.replace(start, "")), + contents: file.contents, + })); - try { - const { id } = await playgroundService.createShare(files); + terminalContext.setBufferedContent( + <> +

Loaded files: {files.map((i) => i.path).join(", ")}

+

+ Generating + share link... +

+ + ); + + const { id: shareId } = await playgroundService.createShare(files); - if (!id) - throw new Error("error"); + if (!shareId) { + throw new Error("Unable to generate share link. Please try again."); + } - terminalContext.setBufferedContent(); + terminalContext.setBufferedContent( + <> +

Share link generated successfully!

+ + + ); } catch (err) { - if (err instanceof Error) - terminalContext.setBufferedContent(

{err.message?.trim() || "There was an error generating the share link. Please try again later."}

); - return; + const errorMessage = + err instanceof Error && err.message + ? err.message.trim() + : "An unexpected error occurred while generating the share link. Please try again."; + + // Log error for debugging + console.error("Share Error:", err); + + // Notify user of error + terminalContext.setBufferedContent( + <> +

Error: {errorMessage}

+ + ); + + // Optional error toast + toast({ + title: "Share Link Error", + description: errorMessage, + }); } }, };