| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- """
- 施工方案审查API错误码统一定义
- 集中管理所有接口的错误码和错误响应格式
- """
- from typing import Dict, Any
- from fastapi import HTTPException
- class ErrorCodes:
- """错误码常量定义"""
- # 文件上传接口错误码 (WJSC001-WJSC008)
- WJSC001 = {
- "code": "WJSC001",
- "error_type": "FILE_MISSING",
- "message": "未上传文件",
- "status_code": 400
- }
- WJSC002 = {
- "code": "WJSC002",
- "error_type": "FILE_MULTIPLE",
- "message": "仅支持单文件上传",
- "status_code": 400
- }
- WJSC003 = {
- "code": "WJSC003",
- "error_type": "FILE_REJECTED",
- "message": "格式错误、内容违规、文件为空",
- "status_code": 400
- }
- WJSC004 = {
- "code": "WJSC004",
- "error_type": "FILE_FORMAT_UNSUPPORTED",
- "message": "文件格式不支持(仅允许pdf/doc/docx)",
- "status_code": 400
- }
- WJSC005 = {
- "code": "WJSC005",
- "error_type": "FILE_SIZE_EXCEEDED",
- "message": "文件过大(最大30MB)",
- "status_code": 400
- }
- WJSC006 = {
- "code": "WJSC006",
- "error_type": "PROJECT_PLAN_TYPE_INVALID",
- "message": "工程方案类型无效(未注册)",
- "status_code": 400
- }
- WJSC007 = {
- "code": "WJSC007",
- "error_type": "UNAUTHORIZED",
- "message": "认证失败(未提供或无效的Authorization)",
- "status_code": 401
- }
- WJSC008 = {
- "code": "WJSC008",
- "error_type": "INVALID_USER",
- "message": "用户标识(user)无效",
- "status_code": 403
- }
- WJSC009 = {
- "code": "WJSC009",
- "error_type": "CALLBACK_URL_MISS",
- "message": "回调客户端地址缺失,请提供回调客户端地址",
- "status_code": 403
- }
- WJSC010 = {
- "code": "WJSC010",
- "error_type": "INTERNAL_ERROR",
- "message": "服务端内部错误",
- "status_code": 500
- }
- # 进度查询接口错误码 (JDLX001-JDLX006)
- JDLX001 = {
- "code": "JDLX001",
- "error_type": "MISSING_PARAMETERS",
- "message": "请求参数缺失",
- "status_code": 400
- }
- JDLX002 = {
- "code": "JDLX002",
- "error_type": "INVALID_PARAM_FORMAT",
- "message": "请求参数格式错误",
- "status_code": 400
- }
- JDLX003 = {
- "code": "JDLX003",
- "error_type": "UNAUTHORIZED",
- "message": "认证失败(未提供或无效的Authorization)",
- "status_code": 401
- }
- JDLX004 = {
- "code": "JDLX004",
- "error_type": "INVALID_USER",
- "message": "用户标识(user)无效",
- "status_code": 403
- }
- JDLX005 = {
- "code": "JDLX005",
- "error_type": "TASK_NOT_FOUND",
- "message": "任务ID不存在或已过期",
- "status_code": 404
- }
- JDLX006 = {
- "code": "JDLX006",
- "error_type": "SERVER_INTERNAL_ERROR",
- "message": "服务端内部错误",
- "status_code": 500
- }
- # 审查结果接口错误码 (SCJG001-SCJG008)
- SCJG001 = {
- "code": "SCJG001",
- "error_type": "INVALID_TYPE",
- "message": "结果类型无效(非'summary'或'issues')",
- "status_code": 400
- }
- SCJG002 = {
- "code": "SCJG002",
- "error_type": "MISSING_PARAM_ID",
- "message": "callback_task_id缺失",
- "status_code": 400
- }
- SCJG003 = {
- "code": "SCJG003",
- "error_type": "INVALID_ID_FORMAT",
- "message": "callback_task_id格式错误",
- "status_code": 400
- }
- SCJG004 = {
- "code": "SCJG004",
- "error_type": "UNAUTHORIZED",
- "message": "认证失败(未提供或无效的Authorization)",
- "status_code": 401
- }
- SCJG005 = {
- "code": "SCJG005",
- "error_type": "INVALID_USER",
- "message": "用户标识无效",
- "status_code": 403
- }
- SCJG006 = {
- "code": "SCJG006",
- "error_type": "TASK_NOT_FOUND",
- "message": "callback_task_id不存在或已过期",
- "status_code": 404
- }
- SCJG007 = {
- "code": "SCJG007",
- "error_type": "NO_REVIEW_RESULTS",
- "message": "无审查结果数据",
- "status_code": 404
- }
- SCJG008 = {
- "code": "SCJG008",
- "error_type": "SERVER_ERROR",
- "message": "服务端内部错误(审查结果生成失败)",
- "status_code": 500
- }
- def create_http_exception(error_code: Dict[str, Any], custom_message: str = None) -> HTTPException:
- """
- 创建HTTP异常
- Args:
- error_code: 错误码字典
- custom_message: 自定义错误消息,可选
- Returns:
- HTTPException: FastAPI异常对象
- """
- detail = {
- "code": error_code["code"],
- "error_type": error_code["error_type"],
- "message": custom_message or error_code["message"]
- }
- return HTTPException(
- status_code=error_code["status_code"],
- detail=detail
- )
- def create_server_error(error_code: str, original_error: Exception) -> HTTPException:
- """
- 创建服务器内部错误异常
- Args:
- error_code: 错误码 (如 "WJSC008", "JDLX006", "SCJG008")
- original_error: 原始异常
- Returns:
- HTTPException: FastAPI异常对象
- """
- error_map = {
- "WJSC008": ErrorCodes.WJSC008,
- "JDLX006": ErrorCodes.JDLX006,
- "SCJG008": ErrorCodes.SCJG008
- }
- error_config = error_map.get(error_code, ErrorCodes.WJSC008)
- message = f"{error_config['message']}: {str(original_error)}"
- return create_http_exception(error_config, message)
- # 便捷的错误创建函数
- class FileUploadErrors:
- """文件上传接口错误"""
- @staticmethod
- def file_missing():
- return create_http_exception(ErrorCodes.WJSC001)
- @staticmethod
- def file_multiple():
- return create_http_exception(ErrorCodes.WJSC002)
- @staticmethod
- def file_rejected(message: str = None):
- return create_http_exception(ErrorCodes.WJSC003, message)
- @staticmethod
- def file_format_unsupported():
- return create_http_exception(ErrorCodes.WJSC004)
- @staticmethod
- def file_size_exceeded():
- return create_http_exception(ErrorCodes.WJSC005)
- @staticmethod
- def project_plan_type_invalid():
- return create_http_exception(ErrorCodes.WJSC006)
- @staticmethod
- def unauthorized():
- return create_http_exception(ErrorCodes.WJSC007)
-
- @staticmethod
- def invalid_user():
- return create_http_exception(ErrorCodes.WJSC008)
-
- @staticmethod
- def callback_url_missing():
- return create_http_exception(ErrorCodes.WJSC009)
- @staticmethod
- def internal_error(original_error: Exception):
- return create_server_error("WJSC010", original_error)
- class TaskProgressErrors:
- """进度查询接口错误"""
- @staticmethod
- def missing_parameters():
- return create_http_exception(ErrorCodes.JDLX001)
- @staticmethod
- def invalid_param_format():
- return create_http_exception(ErrorCodes.JDLX002)
- @staticmethod
- def unauthorized():
- return create_http_exception(ErrorCodes.JDLX003)
- @staticmethod
- def invalid_user():
- return create_http_exception(ErrorCodes.JDLX004)
- @staticmethod
- def task_not_found():
- return create_http_exception(ErrorCodes.JDLX005)
- @staticmethod
- def server_internal_error(original_error: Exception):
- return create_server_error("JDLX006", original_error)
- class ReviewResultsErrors:
- """审查结果接口错误"""
- @staticmethod
- def invalid_type():
- return create_http_exception(ErrorCodes.SCJG001)
- @staticmethod
- def missing_param_id():
- return create_http_exception(ErrorCodes.SCJG002)
- @staticmethod
- def invalid_id_format():
- return create_http_exception(ErrorCodes.SCJG003)
- @staticmethod
- def unauthorized():
- return create_http_exception(ErrorCodes.SCJG004)
- @staticmethod
- def invalid_user():
- return create_http_exception(ErrorCodes.SCJG005)
- @staticmethod
- def task_not_found():
- return create_http_exception(ErrorCodes.SCJG006)
- @staticmethod
- def no_review_results():
- return create_http_exception(ErrorCodes.SCJG007)
- @staticmethod
- def server_error(original_error: Exception):
- return create_server_error("SCJG008", original_error)
|