From 14c5903aef92aac01f5c3101fa42f93acb1946c0 Mon Sep 17 00:00:00 2001 From: sophie <16087569+sophie_liang@user.noreply.gitee.com> Date: Tue, 2 Jun 2026 10:32:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(exception):=20=E4=BC=98=E5=8C=96=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/user.py | 8 +++++--- backend/tests/test_tasks_0304.py | 10 +++++++--- frontend/src/i18n.ts | 12 ++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/backend/app/api/user.py b/backend/app/api/user.py index d8b5e0eb..f78a62f1 100644 --- a/backend/app/api/user.py +++ b/backend/app/api/user.py @@ -632,15 +632,15 @@ def login(): if not user: _write_login_audit('password', 'failed', username=username, reason='user_not_found') db.session.commit() - raise NotFoundError('User not found') + raise AppError(code='AUTH.USER_NOT_FOUND', message='User not found', http_status=404) if not user.check_password(password): _write_login_audit('password', 'failed', user=user, reason='incorrect_password') db.session.commit() - raise UnauthorizedError('Incorrect password') + raise AppError(code='AUTH.INCORRECT_PASSWORD', message='Incorrect password', http_status=401) if user.status != 'active': _write_login_audit('password', 'failed', user=user, reason='account_disabled') db.session.commit() - raise ForbiddenError('Account is disabled') + raise AppError(code='AUTH.ACCOUNT_DISABLED', message='Account is disabled', http_status=403) _write_login_audit('password', 'success', user=user) db.session.commit() return jsonify(_build_login_payload(user, 'password')) @@ -662,6 +662,8 @@ def login(): except (OperationalError, DatabaseError) as retry_exc: current_app.logger.warning('Password login retry failed: %s', retry_exc) raise ServiceUnavailableError('Database connection failed') + except AppError: + raise except Exception as e: db.session.rollback() current_app.logger.exception('Login error: %s', e) diff --git a/backend/tests/test_tasks_0304.py b/backend/tests/test_tasks_0304.py index 6d9e7dac..62cad169 100644 --- a/backend/tests/test_tasks_0304.py +++ b/backend/tests/test_tasks_0304.py @@ -100,8 +100,12 @@ def test_login_success(client): def test_login_fail(client): res = client.post('/api/user/login', json={'username': 'test1', 'password': 'wrongpassword'}) assert res.status_code == 401 - assert 'Incorrect password' in res.get_json()['error'] - + body = res.get_json() + assert body['error']['code'] == 'AUTH.INCORRECT_PASSWORD' + assert 'Incorrect password' in body['error']['message'] + res = client.post('/api/user/login', json={'username': 'nonexistent', 'password': 'password'}) assert res.status_code == 404 - assert 'User not found' in res.get_json()['error'] + body = res.get_json() + assert body['error']['code'] == 'AUTH.USER_NOT_FOUND' + assert 'User not found' in body['error']['message'] diff --git a/frontend/src/i18n.ts b/frontend/src/i18n.ts index b9a988d3..7018cbe1 100644 --- a/frontend/src/i18n.ts +++ b/frontend/src/i18n.ts @@ -156,6 +156,9 @@ const messages = { generic: 'Request failed.', codes: { AUTH_UNAUTHORIZED: 'Please log in first.', + AUTH_INCORRECT_PASSWORD: 'Incorrect password', + AUTH_USER_NOT_FOUND: 'User not found', + AUTH_ACCOUNT_DISABLED: 'Account is disabled', AUTH_FORBIDDEN: 'You do not have permission to perform this action.', RESOURCE_NOT_FOUND: 'The requested resource was not found.', RESOURCE_CONFLICT: 'Request conflict. Please refresh and try again.', @@ -1639,6 +1642,9 @@ const messages = { generic: '请求失败。', codes: { AUTH_UNAUTHORIZED: '请先登录。', + AUTH_INCORRECT_PASSWORD: '密码错误', + AUTH_USER_NOT_FOUND: '用户不存在', + AUTH_ACCOUNT_DISABLED: '账号已禁用', AUTH_FORBIDDEN: '你没有权限执行该操作。', RESOURCE_NOT_FOUND: '请求的资源不存在。', RESOURCE_CONFLICT: '请求冲突,请刷新后重试。', @@ -3086,6 +3092,9 @@ const messages = { generic: '請求失敗。', codes: { AUTH_UNAUTHORIZED: '請先登入。', + AUTH_INCORRECT_PASSWORD: '密碼錯誤', + AUTH_USER_NOT_FOUND: '用戶不存在', + AUTH_ACCOUNT_DISABLED: '帳號已禁用', AUTH_FORBIDDEN: '你沒有權限執行此操作。', RESOURCE_NOT_FOUND: '請求的資源不存在。', RESOURCE_CONFLICT: '請求衝突,請刷新後重試。', @@ -4408,6 +4417,9 @@ const messages = { generic: 'คำขอล้มเหลว', codes: { AUTH_UNAUTHORIZED: 'กรุณาเข้าสู่ระบบก่อน', + AUTH_INCORRECT_PASSWORD: 'รหัสผ่านไม่ถูกต้อง', + AUTH_USER_NOT_FOUND: 'ไม่พบผู้ใช้', + AUTH_ACCOUNT_DISABLED: 'บัญชีถูกปิดใช้งาน', AUTH_FORBIDDEN: 'คุณไม่มีสิทธิ์ดำเนินการนี้', RESOURCE_NOT_FOUND: 'ไม่พบทรัพยากรที่ร้องขอ', RESOURCE_CONFLICT: 'คำขอขัดแย้ง โปรดรีเฟรชแล้วลองใหม่',