fix(cli/run): properly serialize error objects to prevent [object Object] output
- Add serializeError utility to handle Error instances, plain objects, and nested message paths - Fix handleSessionError to use serializeError instead of naive String() conversion - Fix runner.ts catch block to use serializeError for detailed error messages - Add session.error case to logEventVerbose for better error visibility - Add comprehensive tests for serializeError function Fixes error logging in sisyphus-agent workflow where errors were displayed as '[object Object]'
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from "bun:test"
|
||||
import { createEventState, type EventState } from "./events"
|
||||
import { createEventState, serializeError, type EventState } from "./events"
|
||||
import type { RunContext, EventPayload } from "./types"
|
||||
|
||||
const createMockContext = (sessionID: string = "test-session"): RunContext => ({
|
||||
@@ -15,6 +15,63 @@ async function* toAsyncIterable<T>(items: T[]): AsyncIterable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
describe("serializeError", () => {
|
||||
it("returns 'Unknown error' for null/undefined", () => {
|
||||
// #given / #when / #then
|
||||
expect(serializeError(null)).toBe("Unknown error")
|
||||
expect(serializeError(undefined)).toBe("Unknown error")
|
||||
})
|
||||
|
||||
it("returns message from Error instance", () => {
|
||||
// #given
|
||||
const error = new Error("Something went wrong")
|
||||
|
||||
// #when / #then
|
||||
expect(serializeError(error)).toBe("Something went wrong")
|
||||
})
|
||||
|
||||
it("returns string as-is", () => {
|
||||
// #given / #when / #then
|
||||
expect(serializeError("Direct error message")).toBe("Direct error message")
|
||||
})
|
||||
|
||||
it("extracts message from plain object", () => {
|
||||
// #given
|
||||
const errorObj = { message: "Object error message", code: "ERR_001" }
|
||||
|
||||
// #when / #then
|
||||
expect(serializeError(errorObj)).toBe("Object error message")
|
||||
})
|
||||
|
||||
it("extracts message from nested error object", () => {
|
||||
// #given
|
||||
const errorObj = { error: { message: "Nested error message" } }
|
||||
|
||||
// #when / #then
|
||||
expect(serializeError(errorObj)).toBe("Nested error message")
|
||||
})
|
||||
|
||||
it("extracts message from data.message path", () => {
|
||||
// #given
|
||||
const errorObj = { data: { message: "Data error message" } }
|
||||
|
||||
// #when / #then
|
||||
expect(serializeError(errorObj)).toBe("Data error message")
|
||||
})
|
||||
|
||||
it("JSON stringifies object without message property", () => {
|
||||
// #given
|
||||
const errorObj = { code: "ERR_001", status: 500 }
|
||||
|
||||
// #when
|
||||
const result = serializeError(errorObj)
|
||||
|
||||
// #then
|
||||
expect(result).toContain("ERR_001")
|
||||
expect(result).toContain("500")
|
||||
})
|
||||
})
|
||||
|
||||
describe("createEventState", () => {
|
||||
it("creates initial state with correct defaults", () => {
|
||||
// #given / #when
|
||||
|
||||
@@ -11,6 +11,51 @@ import type {
|
||||
ToolResultProps,
|
||||
} from "./types"
|
||||
|
||||
export function serializeError(error: unknown): string {
|
||||
if (!error) return "Unknown error"
|
||||
|
||||
if (error instanceof Error) {
|
||||
const parts = [error.message]
|
||||
if (error.cause) {
|
||||
parts.push(`Cause: ${serializeError(error.cause)}`)
|
||||
}
|
||||
return parts.join(" | ")
|
||||
}
|
||||
|
||||
if (typeof error === "string") {
|
||||
return error
|
||||
}
|
||||
|
||||
if (typeof error === "object") {
|
||||
const obj = error as Record<string, unknown>
|
||||
|
||||
const messagePaths = [
|
||||
obj.message,
|
||||
obj.error,
|
||||
(obj.data as Record<string, unknown>)?.message,
|
||||
(obj.data as Record<string, unknown>)?.error,
|
||||
(obj.error as Record<string, unknown>)?.message,
|
||||
]
|
||||
|
||||
for (const msg of messagePaths) {
|
||||
if (typeof msg === "string" && msg.length > 0) {
|
||||
return msg
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const json = JSON.stringify(error, null, 2)
|
||||
if (json !== "{}") {
|
||||
return json
|
||||
}
|
||||
} catch (_) {
|
||||
void _
|
||||
}
|
||||
}
|
||||
|
||||
return String(error)
|
||||
}
|
||||
|
||||
export interface EventState {
|
||||
mainSessionIdle: boolean
|
||||
mainSessionError: boolean
|
||||
@@ -125,6 +170,13 @@ function logEventVerbose(ctx: RunContext, payload: EventPayload): void {
|
||||
break
|
||||
}
|
||||
|
||||
case "session.error": {
|
||||
const errorProps = props as SessionErrorProps | undefined
|
||||
const errorMsg = serializeError(errorProps?.error)
|
||||
console.error(pc.red(`${sessionTag} ❌ SESSION.ERROR: ${errorMsg}`))
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
console.error(pc.dim(`${sessionTag} ${payload.type}`))
|
||||
}
|
||||
@@ -166,9 +218,7 @@ function handleSessionError(
|
||||
const props = payload.properties as SessionErrorProps | undefined
|
||||
if (props?.sessionID === ctx.sessionID) {
|
||||
state.mainSessionError = true
|
||||
state.lastError = props?.error
|
||||
? String(props.error instanceof Error ? props.error.message : props.error)
|
||||
: "Unknown error"
|
||||
state.lastError = serializeError(props?.error)
|
||||
console.error(pc.red(`\n[session.error] ${state.lastError}`))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createOpencode } from "@opencode-ai/sdk"
|
||||
import pc from "picocolors"
|
||||
import type { RunOptions, RunContext } from "./types"
|
||||
import { checkCompletionConditions } from "./completion"
|
||||
import { createEventState, processEvents } from "./events"
|
||||
import { createEventState, processEvents, serializeError } from "./events"
|
||||
|
||||
const POLL_INTERVAL_MS = 500
|
||||
const DEFAULT_TIMEOUT_MS = 0
|
||||
@@ -115,7 +115,7 @@ export async function run(options: RunOptions): Promise<number> {
|
||||
if (err instanceof Error && err.name === "AbortError") {
|
||||
return 130
|
||||
}
|
||||
console.error(pc.red(`Error: ${err}`))
|
||||
console.error(pc.red(`Error: ${serializeError(err)}`))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user