mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:11:24 +00:00
fix: classify /tools/invoke errors and sanitize 500s (#13185) (thanks @davidrudduck)
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
resolveToolProfilePolicy,
|
||||
stripPluginOnlyAllowlist,
|
||||
} from "../agents/tool-policy.js";
|
||||
import { ToolInputError } from "../agents/tools/common.js";
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import { resolveMainSessionKey } from "../config/sessions.js";
|
||||
import { logWarn } from "../logger.js";
|
||||
@@ -116,6 +117,28 @@ function mergeActionIntoArgsIfSupported(params: {
|
||||
return { ...args, action };
|
||||
}
|
||||
|
||||
function getErrorMessage(err: unknown): string {
|
||||
if (err instanceof Error) {
|
||||
return err.message || String(err);
|
||||
}
|
||||
if (typeof err === "string") {
|
||||
return err;
|
||||
}
|
||||
return String(err);
|
||||
}
|
||||
|
||||
function isToolInputError(err: unknown): boolean {
|
||||
if (err instanceof ToolInputError) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
typeof err === "object" &&
|
||||
err !== null &&
|
||||
"name" in err &&
|
||||
(err as { name?: unknown }).name === "ToolInputError"
|
||||
);
|
||||
}
|
||||
|
||||
export async function handleToolsInvokeHttpRequest(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
@@ -348,6 +371,13 @@ export async function handleToolsInvokeHttpRequest(
|
||||
const result = await (tool as any).execute?.(`http-${Date.now()}`, toolArgs);
|
||||
sendJson(res, 200, { ok: true, result });
|
||||
} catch (err) {
|
||||
if (isToolInputError(err)) {
|
||||
sendJson(res, 400, {
|
||||
ok: false,
|
||||
error: { type: "tool_error", message: getErrorMessage(err) || "invalid tool arguments" },
|
||||
});
|
||||
return true;
|
||||
}
|
||||
logWarn(`tools-invoke: tool execution failed: ${String(err)}`);
|
||||
sendJson(res, 500, {
|
||||
ok: false,
|
||||
|
||||
Reference in New Issue
Block a user