diff --git a/src/gateway/server-methods/chat-transcript-inject.ts b/src/gateway/server-methods/chat-transcript-inject.ts index be83ebe82c2..60fe9a83a9b 100644 --- a/src/gateway/server-methods/chat-transcript-inject.ts +++ b/src/gateway/server-methods/chat-transcript-inject.ts @@ -1,4 +1,5 @@ import { SessionManager } from "@mariozechner/pi-coding-agent"; +import { formatErrorMessage } from "../../infra/errors.js"; import { emitSessionTranscriptUpdate } from "../../sessions/transcript-events.js"; type AppendMessageArg = Parameters[0]; @@ -110,6 +111,6 @@ export function appendInjectedAssistantMessageToTranscript(params: { }); return { ok: true, messageId, message: messageBody }; } catch (err) { - return { ok: false, error: err instanceof Error ? err.message : String(err) }; + return { ok: false, error: formatErrorMessage(err) }; } } diff --git a/src/gateway/server-methods/chat.ts b/src/gateway/server-methods/chat.ts index cd33f70a3cf..86efe6beaff 100644 --- a/src/gateway/server-methods/chat.ts +++ b/src/gateway/server-methods/chat.ts @@ -15,6 +15,7 @@ import type { MsgContext } from "../../auto-reply/templating.js"; import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../../auto-reply/tokens.js"; import type { ReplyPayload } from "../../auto-reply/types.js"; import { resolveSessionFilePath } from "../../config/sessions.js"; +import { formatErrorMessage } from "../../infra/errors.js"; import { jsonUtf8Bytes } from "../../infra/json-utf8-bytes.js"; import type { PromptImageOrderEntry } from "../../media/prompt-image-order.js"; import { type SavedMedia, saveMediaBuffer } from "../../media/store.js"; @@ -859,7 +860,7 @@ function ensureTranscriptFile(params: { transcriptPath: string; sessionId: strin }); return { ok: true }; } catch (err) { - return { ok: false, error: err instanceof Error ? err.message : String(err) }; + return { ok: false, error: formatErrorMessage(err) }; } } diff --git a/src/gateway/server-methods/config.ts b/src/gateway/server-methods/config.ts index a750f1170a1..6691127b80d 100644 --- a/src/gateway/server-methods/config.ts +++ b/src/gateway/server-methods/config.ts @@ -20,6 +20,7 @@ import { loadGatewayRuntimeConfigSchema } from "../../config/runtime-schema.js"; import { lookupConfigSchema, type ConfigSchemaResponse } from "../../config/schema.js"; import { extractDeliveryInfo } from "../../config/sessions.js"; import type { ConfigValidationIssue, OpenClawConfig } from "../../config/types.openclaw.js"; +import { formatErrorMessage } from "../../infra/errors.js"; import { formatDoctorNonInteractiveHint, type RestartSentinelPayload, @@ -275,7 +276,7 @@ async function ensureResolvableSecretRefsOrRespond(params: { }); return true; } catch (error) { - const details = error instanceof Error ? error.message : String(error); + const details = formatErrorMessage(error); params.respond( false, undefined, diff --git a/src/gateway/server-methods/cron.ts b/src/gateway/server-methods/cron.ts index 9ef5dd03019..edcc66a66c8 100644 --- a/src/gateway/server-methods/cron.ts +++ b/src/gateway/server-methods/cron.ts @@ -6,6 +6,7 @@ import { } from "../../cron/run-log.js"; import type { CronJobCreate, CronJobPatch } from "../../cron/types.js"; import { validateScheduleTimestamp } from "../../cron/validate-timestamp.js"; +import { formatErrorMessage } from "../../infra/errors.js"; import { ErrorCodes, errorShape, @@ -105,7 +106,7 @@ export const cronHandlers: GatewayRequestHandlers = { undefined, errorShape( ErrorCodes.INVALID_REQUEST, - `invalid cron.add params: ${err instanceof Error ? err.message : String(err)}`, + `invalid cron.add params: ${formatErrorMessage(err)}`, ), ); return; @@ -145,7 +146,7 @@ export const cronHandlers: GatewayRequestHandlers = { undefined, errorShape( ErrorCodes.INVALID_REQUEST, - `invalid cron.update params: ${err instanceof Error ? err.message : String(err)}`, + `invalid cron.update params: ${formatErrorMessage(err)}`, ), ); return; @@ -249,7 +250,7 @@ export const cronHandlers: GatewayRequestHandlers = { try { result = await context.cron.enqueueRun(jobId, p.mode ?? "force"); } catch (error) { - const message = error instanceof Error ? error.message : String(error); + const message = formatErrorMessage(error); if (message === "invalid cron sessionTarget session id") { respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, message)); return; diff --git a/src/gateway/server-methods/nodes.ts b/src/gateway/server-methods/nodes.ts index 77446ecbd56..4564b238a32 100644 --- a/src/gateway/server-methods/nodes.ts +++ b/src/gateway/server-methods/nodes.ts @@ -1,6 +1,7 @@ import { randomUUID } from "node:crypto"; import { loadConfig } from "../../config/config.js"; import { listDevicePairing } from "../../infra/device-pairing.js"; +import { formatErrorMessage } from "../../infra/errors.js"; import { approveNodePairing, listNodePairing, @@ -352,7 +353,7 @@ export async function maybeWakeNodeWithApns( }); } catch (err) { // Best-effort wake only. - const message = err instanceof Error ? err.message : String(err); + const message = formatErrorMessage(err); if (state.lastWakeAtMs === 0) { return withDuration({ available: false, @@ -451,7 +452,7 @@ export async function maybeSendNodeWakeNudge(nodeId: string): Promise { @@ -171,11 +168,7 @@ export const skillsHandlers: GatewayRequestHandlers = { }); respond(true, detail, undefined); } catch (err) { - respond( - false, - undefined, - errorShape(ErrorCodes.UNAVAILABLE, err instanceof Error ? err.message : String(err)), - ); + respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, formatErrorMessage(err))); } }, "skills.install": async ({ params, respond }) => {