refactor: share feishu api error formatting

This commit is contained in:
Peter Steinberger
2026-04-20 21:58:10 +01:00
parent 66fb12d18a
commit 29a5ab9632
3 changed files with 48 additions and 47 deletions

View File

@@ -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: {

View File

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

View File

@@ -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): {