refactor: dedupe gateway handler error formatting

This commit is contained in:
Peter Steinberger
2026-04-07 01:08:13 +01:00
parent 999d88d13d
commit 533bd00001
7 changed files with 18 additions and 19 deletions

View File

@@ -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<SessionManager["appendMessage"]>[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) };
}
}

View File

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

View File

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

View File

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

View File

@@ -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<NodeWakeNu
apnsReason: result.reason,
});
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
const message = formatErrorMessage(err);
return withDuration({
sent: false,
throttled: false,

View File

@@ -26,6 +26,7 @@ import {
type SessionPatchHookContext,
type SessionPatchHookEvent,
} from "../../hooks/internal-hooks.js";
import { formatErrorMessage } from "../../infra/errors.js";
import {
normalizeAgentId,
parseAgentSessionKey,
@@ -306,7 +307,7 @@ function ensureSessionTranscriptFile(params: {
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err),
error: formatErrorMessage(err),
};
}
}

View File

@@ -16,6 +16,7 @@ import { listAgentWorkspaceDirs } from "../../agents/workspace-dirs.js";
import type { OpenClawConfig } from "../../config/config.js";
import { loadConfig, writeConfigFile } from "../../config/config.js";
import { fetchClawHubSkillDetail } from "../../infra/clawhub.js";
import { formatErrorMessage } from "../../infra/errors.js";
import { getRemoteSkillEligibility } from "../../infra/skills-remote.js";
import { normalizeAgentId } from "../../routing/session-key.js";
import { normalizeSecretInput } from "../../utils/normalize-secret-input.js";
@@ -146,11 +147,7 @@ export const skillsHandlers: GatewayRequestHandlers = {
});
respond(true, { results }, 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.detail": async ({ params, respond }) => {
@@ -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 }) => {