From 057a00257dff3db9fa9b773de81ef5eb1b47a1c2 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 16 Jul 2025 09:48:46 -0700 Subject: [PATCH] Add error formatting utility to handle non-standard error objects --- src/commands/chat.ts | 7 +++--- src/ui/hooks/useChat.ts | 3 ++- src/util/formatError.ts | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/util/formatError.ts diff --git a/src/commands/chat.ts b/src/commands/chat.ts index aaa6bd936..a328bc30b 100644 --- a/src/commands/chat.ts +++ b/src/commands/chat.ts @@ -9,6 +9,7 @@ import { loadSession, saveSession } from "../session.js"; import { streamChatResponse } from "../streamChatResponse.js"; import { constructSystemMessage } from "../systemMessage.js"; import { startTUIChat } from "../ui/index.js"; +import { formatError } from "../util/formatError.js"; export interface ChatOptions { headless?: boolean; @@ -128,7 +129,7 @@ export async function chat(prompt?: string, options: ChatOptions = {}) { // Save session after each successful response saveSession(chatHistory); } catch (e: any) { - console.error(`\n${chalk.red(`Error: ${e.message}`)}`); + console.error(`\n${chalk.red(`Error: ${formatError(e)}`)}`); if (!options.headless) { console.info( chalk.dim(`Chat history:\n${JSON.stringify(chatHistory, null, 2)}`) @@ -137,7 +138,7 @@ export async function chat(prompt?: string, options: ChatOptions = {}) { } } } catch (error: any) { - console.error(chalk.red(`Fatal error: ${error.message}`)); + console.error(chalk.red(`Fatal error: ${formatError(error)}`)); process.exit(1); } -} +} \ No newline at end of file diff --git a/src/ui/hooks/useChat.ts b/src/ui/hooks/useChat.ts index 5bb0a4bcf..0928f6eec 100644 --- a/src/ui/hooks/useChat.ts +++ b/src/ui/hooks/useChat.ts @@ -12,6 +12,7 @@ import { } from "../../streamChatResponse.js"; import { constructSystemMessage } from "../../systemMessage.js"; import { getToolDisplayName } from "../../tools.js"; +import { formatError } from "../../util/formatError.js"; import { DisplayMessage } from "../types.js"; @@ -296,7 +297,7 @@ export function useChat({ // Save the updated history to session saveSession(finalHistory); } catch (error: any) { - const errorMessage = `Error: ${error.message}`; + const errorMessage = `Error: ${formatError(error)}`; setMessages((prev) => [ ...prev, { diff --git a/src/util/formatError.ts b/src/util/formatError.ts new file mode 100644 index 000000000..85e555ce5 --- /dev/null +++ b/src/util/formatError.ts @@ -0,0 +1,53 @@ +/** + * Safely formats an error object into a readable string + */ +export function formatError(error: any): string { + if (error instanceof Error) { + return error.message; + } + + if (typeof error === 'string') { + return error; + } + + if (error && typeof error === 'object') { + // Try to extract common error properties + if (error.message) { + return error.message; + } + if (error.error) { + return formatError(error.error); + } + if (error.details) { + return formatError(error.details); + } + if (error.description) { + return error.description; + } + + // For API errors, try to extract meaningful info + if (error.status && error.error && error.error.message) { + return `HTTP ${error.status}: ${error.error.message}`; + } + + // For network errors + if (error.code && error.syscall) { + return `Network error: ${error.code} in ${error.syscall}`; + } + + // For errors with an errors array + if (error.errors && Array.isArray(error.errors)) { + return error.errors.join(', '); + } + + // Try to JSON stringify if possible + try { + return JSON.stringify(error); + } catch { + // If JSON.stringify fails, return a generic message + return `An error occurred: ${Object.prototype.toString.call(error)}`; + } + } + + return String(error); +} \ No newline at end of file