Skip to content

Commit b96402e

Browse files
authored
Simplify custom response status codes (#686)
1 parent 0bc6f6a commit b96402e

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

backend/app/admin/api/v1/sys/plugin.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from backend.app.admin.service.plugin_service import plugin_service
1010
from backend.common.enums import PluginType
11-
from backend.common.response.response_code import CustomResponseCode
11+
from backend.common.response.response_code import CustomResponse
1212
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
1313
from backend.common.security.jwt import DependsJwtAuth
1414
from backend.common.security.permission import RequestPermission
@@ -44,7 +44,9 @@ async def install_plugin(
4444
repo_url: Annotated[str | None, Query(description='插件 git 仓库地址')] = None,
4545
) -> ResponseModel:
4646
await plugin_service.install(type=type, file=file, repo_url=repo_url)
47-
return response_base.success(res=CustomResponseCode.PLUGIN_INSTALL_SUCCESS)
47+
return response_base.success(
48+
res=CustomResponse(code=200, msg='插件安装成功,请根据插件说明(README.md)进行相关配置并重启服务')
49+
)
4850

4951

5052
@router.delete(
@@ -58,7 +60,9 @@ async def install_plugin(
5860
)
5961
async def uninstall_plugin(plugin: Annotated[str, Path(description='插件名称')]) -> ResponseModel:
6062
await plugin_service.uninstall(plugin=plugin)
61-
return response_base.success(res=CustomResponseCode.PLUGIN_UNINSTALL_SUCCESS)
63+
return response_base.success(
64+
res=CustomResponse(code=200, msg='插件卸载成功,请根据插件说明(README.md)移除相关配置并重启服务')
65+
)
6266

6367

6468
@router.put(

backend/common/exception/exception_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def _validation_exception_handler(request: Request, exc: RequestValidation
7676
}
7777
request.state.__request_validation_exception__ = content # 用于在中间件中获取异常信息
7878
content.update(trace_id=get_request_trace_id(request))
79-
return MsgSpecJSONResponse(status_code=422, content=content)
79+
return MsgSpecJSONResponse(status_code=StandardResponseCode.HTTP_422, content=content)
8080

8181

8282
def register_exception(app: FastAPI):

backend/common/response/response_code.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,8 @@ class CustomResponseCode(CustomCodeBase):
2323
"""自定义响应状态码"""
2424

2525
HTTP_200 = (200, '请求成功')
26-
HTTP_201 = (201, '新建请求成功')
27-
HTTP_202 = (202, '请求已接受,但处理尚未完成')
28-
HTTP_204 = (204, '请求成功,但没有返回内容')
2926
HTTP_400 = (400, '请求错误')
30-
HTTP_401 = (401, '未经授权')
31-
HTTP_403 = (403, '禁止访问')
32-
HTTP_404 = (404, '请求的资源不存在')
33-
HTTP_410 = (410, '请求的资源已永久删除')
34-
HTTP_422 = (422, '请求参数非法')
35-
HTTP_425 = (425, '无法执行请求,由于服务器无法满足要求')
36-
HTTP_429 = (429, '请求过多,服务器限制')
3727
HTTP_500 = (500, '服务器内部错误')
38-
HTTP_502 = (502, '网关错误')
39-
HTTP_503 = (503, '服务器暂时无法处理请求')
40-
HTTP_504 = (504, '网关超时')
41-
42-
# Plugin
43-
PLUGIN_INSTALL_SUCCESS = (200, '插件安装成功,请根据插件说明(README.md)进行相关配置并重启服务')
44-
PLUGIN_UNINSTALL_SUCCESS = (200, '插件卸载成功,请根据插件说明(README.md)移除相关配置并重启服务')
4528

4629

4730
class CustomErrorCode(CustomCodeBase):

backend/utils/health_check.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from backend.common.exception import errors
1414
from backend.common.log import log
15+
from backend.common.response.response_code import StandardResponseCode
1516

1617

1718
def ensure_unique_route_names(app: FastAPI) -> None:
@@ -39,7 +40,9 @@ async def http_limit_callback(request: Request, response: Response, expire: int)
3940
:return:
4041
"""
4142
expires = ceil(expire / 1000)
42-
raise errors.HTTPError(code=429, msg='请求过于频繁,请稍后重试', headers={'Retry-After': str(expires)})
43+
raise errors.HTTPError(
44+
code=StandardResponseCode.HTTP_429, msg='请求过于频繁,请稍后重试', headers={'Retry-After': str(expires)}
45+
)
4346

4447

4548
def timer(func) -> Callable:

0 commit comments

Comments
 (0)