Skip to content

Commit d67b108

Browse files
authored
update token default exception return message (#65)
1 parent 847d722 commit d67b108

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

backend/app/common/exception/errors.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from fastapi import HTTPException
66

7-
from backend.app.common.response.response_code import CodeEnum
7+
from backend.app.common.response.response_code import CustomCode
88

99

1010
class BaseExceptionMixin(Exception):
@@ -16,7 +16,14 @@ def __init__(self, *, msg: str = None, data: Any = None):
1616

1717

1818
class HTTPError(HTTPException):
19-
pass
19+
def __init__(self, *, code: int, msg: Any = None, headers: dict[str, Any] | None = None):
20+
super().__init__(status_code=code, detail=msg, headers=headers)
21+
22+
23+
class CustomError(BaseExceptionMixin):
24+
def __init__(self, *, error: CustomCode, data: Any = None):
25+
self.code = error.code
26+
super().__init__(msg=error.msg, data=data)
2027

2128

2229
class RequestError(BaseExceptionMixin):
@@ -54,21 +61,15 @@ def __init__(self, *, msg: str = 'Bad Gateway', data: Any = None):
5461
super().__init__(msg=msg, data=data)
5562

5663

57-
class CodeError(BaseExceptionMixin):
58-
def __init__(self, *, error: CodeEnum, data: Any = None):
59-
self.code = error.code
60-
super().__init__(msg=error.msg, data=data)
61-
62-
6364
class AuthorizationError(BaseExceptionMixin):
6465
code = 401
6566

6667
def __init__(self, *, msg: str = 'Permission denied', data: Any = None):
6768
super().__init__(msg=msg, data=data)
6869

6970

70-
class TokenError(BaseExceptionMixin):
71+
class TokenError(HTTPError):
7172
code = 401
7273

73-
def __init__(self, *, msg: str = 'Token is invalid', data: Any = None):
74-
super().__init__(msg=msg, data=data)
74+
def __init__(self, *, msg: str = 'Not authenticated', headers: dict[str, Any] | None = None):
75+
super().__init__(code=self.code, msg=msg, headers=headers or {'WWW-Authenticate': 'Bearer'})

backend/app/common/response/response_code.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from enum import Enum
44

55

6-
class CodeEnum(Enum):
6+
class CustomCode(Enum):
77
"""
8-
错误码
8+
自定义错误码
99
"""
1010

1111
CAPTCHA_ERROR = (40001, '图形验证码错误')

0 commit comments

Comments
 (0)