Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(bot): explain Feishu 41050 in getMemberInfo instead of a 500 (#21525)

Rdmclin2a47a19800b1e
diff --git a/apps/server/src/services/bot/platforms/feishu/service.test.ts b/apps/server/src/services/bot/platforms/feishu/service.test.ts
index 5f1f20828cc..1d77c26aafb 100644
--- a/apps/server/src/services/bot/platforms/feishu/service.test.ts
+++ b/apps/server/src/services/bot/platforms/feishu/service.test.ts
@@ -149,6 +149,37 @@ describe('FeishuMessageService.readMessages', () => {
});
});
+describe('FeishuMessageService.getMemberInfo', () => {
+ const failWith = (message: string) =>
+ new FeishuMessageService(
+ { getUserInfo: vi.fn().mockRejectedValue(new Error(message)) } as any,
+ 'feishu',
+ );
+
+ it('reports a user outside the contact range as a precondition, naming the fix', async () => {
+ const error = await failWith(
+ 'Lark API GET /contact/v3/users/ou_1?user_id_type=open_id failed: 400 {"code":41050,"msg":"no user authority error"}',
+ )
+ .getMemberInfo({ memberId: 'ou_1', platform: 'feishu' } as any)
+ .catch((e) => e);
+
+ expect(error.code).toBe('PRECONDITION_FAILED');
+ expect(error.message).toContain('41050');
+ expect(error.message).toContain('contacts permission scope');
+ });
+
+ it('leaves unrecognized failures untouched', async () => {
+ // The open_id contains the digits, which must not read as the error code.
+ const message = 'Lark API GET /contact/v3/users/ou_41050?user_id_type=open_id failed: 500 boom';
+ const error = await failWith(message)
+ .getMemberInfo({ memberId: 'ou_41050', platform: 'feishu' } as any)
+ .catch((e) => e);
+
+ expect(error.code).toBeUndefined();
+ expect(error.message).toBe(message);
+ });
+});
+
describe('FeishuMessageService.readDocument', () => {
const makeDocService = (api: Record<string, ReturnType<typeof vi.fn>>) =>
new FeishuMessageService(api as any, 'feishu');
diff --git a/apps/server/src/services/bot/platforms/feishu/service.ts b/apps/server/src/services/bot/platforms/feishu/service.ts
index 1ceae9ed144..90182bb488a 100644
--- a/apps/server/src/services/bot/platforms/feishu/service.ts
+++ b/apps/server/src/services/bot/platforms/feishu/service.ts
@@ -45,6 +45,7 @@ import {
toFeishuEmojiType,
} from '@lobechat/chat-adapter-feishu';
import { DEFAULT_BOT_HISTORY_LIMIT } from '@lobechat/const';
+import { TRPCError } from '@trpc/server';
import type { MessageRuntimeService } from '@/server/services/toolExecution/serverRuntimes/message/adapters/types';
import { PlatformUnsupportedError } from '@/server/services/toolExecution/serverRuntimes/message/PlatformUnsupportedError';
@@ -142,6 +143,32 @@ const explainDocumentReadError = (error: unknown): Error => {
return new Error(`${message} β€” ${DOCUMENT_READ_ERROR_HINTS[code]}.`);
};
+/**
+ * `41050 no user authority error` from `/contact/v3/users/:id` means the user
+ * is outside the app's contacts permission scope, which the bot owner sets in
+ * their own developer console β€” not a server fault. Raised as a 4xx so it
+ * reaches the model as an actionable message instead of an opaque 500. The
+ * hint is model-facing tool output (no UI renders it), so it stays English and
+ * the model relays it in the user's language.
+ *
+ * @see https://open.feishu.cn/document/server-docs/contact-v3/user/get
+ */
+// Anchored on the code's position in `LarkApiClient`'s message (`failed: 400
+// {"code":41050,…}` or `failed: 41050 …`) so an open_id containing the digits
+// cannot match.
+const MEMBER_INFO_NO_AUTHORITY = /(?:"code":|failed: )41050\b/;
+
+const explainMemberInfoError = (error: unknown): Error => {
+ const message = error instanceof Error ? error.message : String(error);
+ if (!MEMBER_INFO_NO_AUTHORITY.test(message))
+ return error instanceof Error ? error : new Error(message);
+ return new TRPCError({
+ cause: error,
+ code: 'PRECONDITION_FAILED',
+ message: `${message} β€” this user is outside the app's contacts permission scope. In the Feishu/Lark developer console, widen the app's contacts data range to include them (or all members), then create and publish a new app version for it to take effect. Users from another tenant cannot be looked up.`,
+ });
+};
+
/**
* What a `readDocument` call points at. A bare `documentId` is assumed to be
* a docx token β€” that is what every other docx endpoint takes and what the
@@ -356,7 +383,9 @@ export class FeishuMessageService implements MessageRuntimeService {
// ==================== Member Information ====================
getMemberInfo = async (params: GetMemberInfoParams): Promise<GetMemberInfoState> => {
- const user = await this.api.getUserInfo(params.memberId);
+ const user = await this.api.getUserInfo(params.memberId).catch((error) => {
+ throw explainMemberInfoError(error);
+ });
return {
displayName: user?.name ?? undefined,
id: params.memberId,