diff --git a/extensions/feishu/src/comment-reaction.ts b/extensions/feishu/src/comment-reaction.ts index 3dd054de9eb..8725522a0b0 100644 --- a/extensions/feishu/src/comment-reaction.ts +++ b/extensions/feishu/src/comment-reaction.ts @@ -1,7 +1,7 @@ import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js"; import { resolveFeishuRuntimeAccount } from "./accounts.js"; import { createFeishuClient } from "./client.js"; -import { encodeQuery, isRecord, readString } from "./comment-shared.js"; +import { encodeQuery, formatFeishuApiError } from "./comment-shared.js"; import { parseFeishuCommentTarget, type CommentFileType } from "./comment-target.js"; const COMMENT_TYPING_REACTION_TYPE = "Typing"; @@ -95,29 +95,7 @@ async function requestCommentTypingReactionWithClient(params: { } function formatCommentReactionFailure(error: unknown): string { - if (!isRecord(error)) { - return typeof error === "string" ? error : JSON.stringify(error); - } - const response = isRecord(error.response) ? error.response : undefined; - const responseData = isRecord(response?.data) ? response?.data : undefined; - return JSON.stringify({ - message: - typeof error.message === "string" - ? error.message - : typeof error === "string" - ? error - : JSON.stringify(error), - code: readString(error.code), - method: readString(isRecord(error.config) ? error.config.method : undefined), - url: readString(isRecord(error.config) ? error.config.url : undefined), - http_status: typeof response?.status === "number" ? response.status : undefined, - feishu_code: - typeof responseData?.code === "number" ? responseData.code : readString(responseData?.code), - feishu_msg: readString(responseData?.msg), - feishu_log_id: - readString(responseData?.log_id) || - readString(isRecord(responseData?.error) ? responseData.error.log_id : undefined), - }); + return formatFeishuApiError(error, { includeNestedErrorLogId: true }); } async function requestCommentTypingReaction(params: { diff --git a/extensions/feishu/src/comment-shared.ts b/extensions/feishu/src/comment-shared.ts index 5b210af640a..1ae0976a687 100644 --- a/extensions/feishu/src/comment-shared.ts +++ b/extensions/feishu/src/comment-shared.ts @@ -29,6 +29,44 @@ export const asRecord = asOptionalRecord; export const hasNonEmptyString = sharedHasNonEmptyString; +export function formatFeishuApiError( + error: unknown, + options: { + includeConfigParams?: boolean; + includeNestedErrorLogId?: boolean; + } = {}, +): string { + if (!isRecord(error)) { + return typeof error === "string" ? error : JSON.stringify(error); + } + const config = isRecord(error.config) ? error.config : undefined; + const response = isRecord(error.response) ? error.response : undefined; + const responseData = isRecord(response?.data) ? response?.data : undefined; + const feishuLogId = + readString(responseData?.log_id) || + (options.includeNestedErrorLogId + ? readString(isRecord(responseData?.error) ? responseData.error.log_id : undefined) + : undefined); + + return JSON.stringify({ + message: + typeof error.message === "string" + ? error.message + : typeof error === "string" + ? error + : JSON.stringify(error), + code: readString(error.code), + method: readString(config?.method), + url: readString(config?.url), + ...(options.includeConfigParams ? { params: config?.params } : {}), + http_status: typeof response?.status === "number" ? response.status : undefined, + feishu_code: + typeof responseData?.code === "number" ? responseData.code : readString(responseData?.code), + feishu_msg: readString(responseData?.msg), + feishu_log_id: feishuLogId, + }); +} + export type ParsedCommentDocumentRef = { fileType?: CommentFileType; fileToken?: string; diff --git a/extensions/feishu/src/drive.ts b/extensions/feishu/src/drive.ts index e300a7ca384..9861d828fa2 100644 --- a/extensions/feishu/src/drive.ts +++ b/extensions/feishu/src/drive.ts @@ -3,7 +3,13 @@ import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; import type { OpenClawPluginApi } from "../runtime-api.js"; import { listEnabledFeishuAccounts } from "./accounts.js"; import { cleanupAmbientCommentTypingReaction } from "./comment-reaction.js"; -import { encodeQuery, extractReplyText, isRecord, readString } from "./comment-shared.js"; +import { + encodeQuery, + extractReplyText, + formatFeishuApiError, + isRecord, + readString, +} from "./comment-shared.js"; import { parseFeishuCommentTarget, type CommentFileType } from "./comment-target.js"; import { FeishuDriveSchema, type FeishuDriveParams } from "./drive-schema.js"; import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js"; @@ -266,28 +272,7 @@ function applyCommentFileTypeDefault< } function formatDriveApiError(error: unknown): string { - if (!isRecord(error)) { - return typeof error === "string" ? error : JSON.stringify(error); - } - const response = isRecord(error.response) ? error.response : undefined; - const responseData = isRecord(response?.data) ? response?.data : undefined; - return JSON.stringify({ - message: - typeof error.message === "string" - ? error.message - : typeof error === "string" - ? error - : JSON.stringify(error), - code: readString(error.code), - method: readString(isRecord(error.config) ? error.config.method : undefined), - url: readString(isRecord(error.config) ? error.config.url : undefined), - params: isRecord(error.config) ? error.config.params : undefined, - http_status: typeof response?.status === "number" ? response.status : undefined, - feishu_code: - typeof responseData?.code === "number" ? responseData.code : readString(responseData?.code), - feishu_msg: readString(responseData?.msg), - feishu_log_id: readString(responseData?.log_id), - }); + return formatFeishuApiError(error, { includeConfigParams: true }); } function extractDriveApiErrorMeta(error: unknown): {