From e0fa31c9d6d034915cf49dda3cb1a561909978c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Tue, 17 Nov 2026 17:17:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(settings):=20=E7=9B=AE=E5=BD=95=E9=89=B4?= =?UTF-8?q?=E6=9D=83=E5=A4=B1=E8=B4=A5=E6=8F=90=E7=A4=BA=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 目录检查不可用时给出 DWS 登录与权限检查说明,避免直接展示机器错误码。 新增真实 Response 回归已先失败复现,修复后完整 make verify 通过。 --- web/src/dwsLogin.test.ts | 9 +++++++++ web/src/dwsLogin.ts | 10 ++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/web/src/dwsLogin.test.ts b/web/src/dwsLogin.test.ts index be593ab..8c0e641 100644 --- a/web/src/dwsLogin.test.ts +++ b/web/src/dwsLogin.test.ts @@ -2,6 +2,15 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { checkDWSConnection, fetchDWSCredentials, startDWSLogin, validDWSAuthorizationURL } from './dwsLogin' afterEach(() => vi.unstubAllGlobals()) describe('DWS transport boundaries', () => { + it('explains missing directory authorization with the new login entry', async () => { + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockResolvedValue(new Response(JSON.stringify({ error: 'dingtalk_discovery_unavailable' }), { status: 503 })) + ) + await expect(checkDWSConnection()).rejects.toThrow('请先登录授权') + }) it('accepts only the official device verification URL', () => { expect(validDWSAuthorizationURL('https://login.dingtalk.com/oauth2/device/verify.htm?user_code=TEST')).toBe(true) for (const value of [ diff --git a/web/src/dwsLogin.ts b/web/src/dwsLogin.ts index 4aa4c4d..f2eb9eb 100644 --- a/web/src/dwsLogin.ts +++ b/web/src/dwsLogin.ts @@ -41,10 +41,12 @@ async function request(path: string, signal?: AbortSignal, method = 'GET') { headers: { Accept: 'application/json', ...(method !== 'GET' ? { 'X-Governance-Action': 'dws-login' } : {}) }, }) const payload: unknown = await response.json().catch(() => null) - if (!response.ok) - throw new Error( - record(payload) && typeof payload.error === 'string' ? payload.error : `DWS 请求失败(HTTP ${response.status})` - ) + if (!response.ok) { + const reason = record(payload) && typeof payload.error === 'string' ? payload.error : '' + if (reason === 'dingtalk_discovery_unavailable') + throw new Error('DWS 未登录、授权已过期或缺少目录权限;请先登录授权,再检查连接。') + throw new Error(reason || `DWS 请求失败(HTTP ${response.status})`) + } if (!record(payload) || !record(payload.data)) throw new Error('DWS 返回的数据格式不正确,请重试') return payload.data }