fix(exception): 优化异常错误处理

This commit is contained in:
sophie committed 2026-06-02 10:32:44 +08:00
1 parent 2a036764e3
commit 14c5903aef
3 files changed
+24 -6

No files matched your search

+5 -3
View File
@@ -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)
+7 -3
View File
@@ -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']
+12
View File
@@ -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: 'คำขอขัดแย้ง โปรดรีเฟรชแล้วลองใหม่',