Merge pull request #50 from continuedev/fix-error-object-object
Add error formatting utility to handle non-standard error objects
This commit is contained in:
commit
8653dbd1e4
3 changed files with 59 additions and 4 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
|
||||
|
|
@ -311,7 +312,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
53
src/util/formatError.ts
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue