Add error formatting utility to handle non-standard error objects

This commit is contained in:
Nate 2025-07-16 09:48:46 -07:00
parent 6f20adf977
commit 057a00257d
3 changed files with 59 additions and 4 deletions

View file

@ -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);
}
}
}

View file

@ -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,
{

53
src/util/formatError.ts Normal file
View file

@ -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);
}