error_schemas.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. """
  2. 施工方案审查API错误码统一定义
  3. 集中管理所有接口的错误码和错误响应格式
  4. """
  5. from typing import Dict, Any
  6. from fastapi import HTTPException
  7. from foundation.logger.loggering import server_logger as logger
  8. class ErrorCodes:
  9. """错误码常量定义"""
  10. # 文件上传接口错误码 (WJSC001-WJSC008)
  11. WJSC001 = {
  12. "code": "WJSC001",
  13. "error_type": "FILE_MISSING",
  14. "message": "未上传文件",
  15. "status_code": 400
  16. }
  17. WJSC002 = {
  18. "code": "WJSC002",
  19. "error_type": "FILE_MULTIPLE",
  20. "message": "仅支持单文件上传",
  21. "status_code": 400
  22. }
  23. WJSC003 = {
  24. "code": "WJSC003",
  25. "error_type": "FILE_REJECTED",
  26. "message": "格式错误、内容违规、文件为空",
  27. "status_code": 400
  28. }
  29. WJSC004 = {
  30. "code": "WJSC004",
  31. "error_type": "FILE_FORMAT_UNSUPPORTED",
  32. "message": "文件格式不支持(仅允许pdf/doc/docx)",
  33. "status_code": 400
  34. }
  35. WJSC005 = {
  36. "code": "WJSC005",
  37. "error_type": "FILE_SIZE_EXCEEDED",
  38. "message": "文件过大(最大不超过50MB)",
  39. "status_code": 400
  40. }
  41. WJSC007 = {
  42. "code": "WJSC007",
  43. "error_type": "UNAUTHORIZED",
  44. "message": "认证失败(未提供或无效的Authorization)",
  45. "status_code": 401
  46. }
  47. WJSC008 = {
  48. "code": "WJSC008",
  49. "error_type": "INVALID_USER",
  50. "message": "用户标识未提供或无效",
  51. "status_code": 403
  52. }
  53. WJSC009 = {
  54. "code": "WJSC009",
  55. "error_type": "INVALID_PARAMETERS",
  56. "message": "请求参数无效或不支持",
  57. "status_code": 400
  58. }
  59. WJSC011 = {
  60. "code": "WJSC011",
  61. "error_type": "INTERNAL_ERROR",
  62. "message": "服务端内部错误",
  63. "status_code": 500
  64. }
  65. # 启动审查接口错误码 (QDSC001-QDSC006)
  66. QDSC001 = {
  67. "code": "QDSC001",
  68. "error_type": "MISSING_PARAMETERS",
  69. "message": "请求参数缺失",
  70. "status_code": 400
  71. }
  72. QDSC002 = {
  73. "code": "QDSC002",
  74. "error_type": "INVALID_PARAM_FORMAT",
  75. "message": "请求参数格式错误",
  76. "status_code": 400
  77. }
  78. QDSC003 = {
  79. "code": "QDSC003",
  80. "error_type": "UNAUTHORIZED",
  81. "message": "认证失败(未提供或无效的Authorization)",
  82. "status_code": 401
  83. }
  84. QDSC004 = {
  85. "code": "QDSC004",
  86. "error_type": "INVALID_USER",
  87. "message": "用户标识未提供或无效",
  88. "status_code": 403
  89. }
  90. QDSC005 = {
  91. "code": "QDSC005",
  92. "error_type": "TASK_NOT_FOUND",
  93. "message": "任务ID不存在或已过期",
  94. "status_code": 404
  95. }
  96. QDSC006 = {
  97. "code": "QDSC006",
  98. "error_type": "TASK_ALREADY_EXISTS",
  99. "message": "任务已存在,请勿重复提交",
  100. "status_code": 409
  101. }
  102. QDSC007 = {
  103. "code": "QDSC007",
  104. "error_type": "PROJECT_PLAN_TYPE_INVALID",
  105. "message": "无效工程方案类型(未提供或未注册)",
  106. "status_code": 400
  107. }
  108. QDSC008 = {
  109. "code": "QDSC008",
  110. "error_type": "ENUM_TYPE_INVALID",
  111. "message": "审查枚举类型无效",
  112. "status_code": 400
  113. }
  114. QDSC009 = {
  115. "code": "QDSC009",
  116. "error_type": "ENUM_TYPE_CANNOT_BE_NULL",
  117. "message": "审查枚举类型不能为空",
  118. "status_code": 400
  119. }
  120. QDSC010 = {
  121. "code": "QDSC010",
  122. "error_type": "FILE_INFO_NOT_FOUND",
  123. "message": "文件信息获取失败",
  124. "status_code": 500
  125. }
  126. QDSC011 = {
  127. "code": "QDSC011",
  128. "error_type": "SERVER_INTERNAL_ERROR",
  129. "message": "服务端内部错误",
  130. "status_code": 500
  131. }
  132. # 审查结果接口错误码 (SCJG001-SCJG008)
  133. SCJG001 = {
  134. "code": "SCJG001",
  135. "error_type": "INVALID_TYPE",
  136. "message": "结果类型无效(非'summary'或'issues')",
  137. "status_code": 400
  138. }
  139. SCJG002 = {
  140. "code": "SCJG002",
  141. "error_type": "MISSING_PARAM_ID",
  142. "message": "callback_task_id缺失",
  143. "status_code": 400
  144. }
  145. SCJG003 = {
  146. "code": "SCJG003",
  147. "error_type": "INVALID_ID_FORMAT",
  148. "message": "callback_task_id格式错误",
  149. "status_code": 400
  150. }
  151. SCJG004 = {
  152. "code": "SCJG004",
  153. "error_type": "UNAUTHORIZED",
  154. "message": "认证失败(未提供或无效的Authorization)",
  155. "status_code": 401
  156. }
  157. SCJG005 = {
  158. "code": "SCJG005",
  159. "error_type": "INVALID_USER",
  160. "message": "用户标识未提供或无效",
  161. "status_code": 403
  162. }
  163. SCJG006 = {
  164. "code": "SCJG006",
  165. "error_type": "TASK_NOT_FOUND",
  166. "message": "callback_task_id不存在或已过期",
  167. "status_code": 404
  168. }
  169. SCJG007 = {
  170. "code": "SCJG007",
  171. "error_type": "NO_REVIEW_RESULTS",
  172. "message": "无审查结果数据",
  173. "status_code": 404
  174. }
  175. SCJG008 = {
  176. "code": "SCJG008",
  177. "error_type": "SERVER_ERROR",
  178. "message": "服务端内部错误(审查结果生成失败)",
  179. "status_code": 500
  180. }
  181. def create_http_exception(error_code: Dict[str, Any], custom_message: str = None) -> HTTPException:
  182. """
  183. 创建HTTP异常
  184. Args:
  185. error_code: 错误码字典
  186. custom_message: 自定义错误消息,可选
  187. Returns:
  188. HTTPException: FastAPI异常对象
  189. """
  190. detail = {
  191. "code": error_code["code"],
  192. "error_type": error_code["error_type"],
  193. "message": custom_message or error_code["message"]
  194. }
  195. return HTTPException(
  196. status_code=error_code["status_code"],
  197. detail=detail
  198. )
  199. def create_server_error(error_code: str, original_error: Exception) -> HTTPException:
  200. """
  201. 创建服务器内部错误异常
  202. Args:
  203. error_code: 错误码 (如 "WJSC008", "QDSC006", "SCJG008")
  204. original_error: 原始异常
  205. Returns:
  206. HTTPException: FastAPI异常对象
  207. """
  208. error_map = {
  209. "WJSC011": ErrorCodes.WJSC011,
  210. "QDSC006": ErrorCodes.QDSC006,
  211. "SCJG008": ErrorCodes.SCJG008
  212. }
  213. error_config = error_map.get(error_code, ErrorCodes.WJSC008)
  214. message = f"{error_config['message']}: {str(original_error)}"
  215. return create_http_exception(error_config, message)
  216. # 便捷的错误创建函数
  217. class FileUploadErrors:
  218. """文件上传接口错误"""
  219. @staticmethod
  220. def file_missing():
  221. logger.error(ErrorCodes.WJSC001)
  222. return create_http_exception(ErrorCodes.WJSC001)
  223. @staticmethod
  224. def file_multiple():
  225. logger.error(ErrorCodes.WJSC002)
  226. return create_http_exception(ErrorCodes.WJSC002)
  227. @staticmethod
  228. def file_rejected(message: str = None):
  229. logger.error(ErrorCodes.WJSC003)
  230. return create_http_exception(ErrorCodes.WJSC003, message)
  231. @staticmethod
  232. def file_format_unsupported():
  233. logger.error(ErrorCodes.WJSC004)
  234. return create_http_exception(ErrorCodes.WJSC004)
  235. @staticmethod
  236. def file_size_exceeded():
  237. logger.error(ErrorCodes.WJSC005)
  238. return create_http_exception(ErrorCodes.WJSC005)
  239. @staticmethod
  240. def project_plan_type_invalid():
  241. logger.error(ErrorCodes.WJSC006)
  242. return create_http_exception(ErrorCodes.WJSC006)
  243. @staticmethod
  244. def unauthorized():
  245. logger.error(ErrorCodes.WJSC007)
  246. return create_http_exception(ErrorCodes.WJSC007)
  247. @staticmethod
  248. def invalid_user():
  249. logger.error(ErrorCodes.WJSC008)
  250. return create_http_exception(ErrorCodes.WJSC008)
  251. @staticmethod
  252. def task_already_exists():
  253. logger.error(ErrorCodes.WJSC010)
  254. return create_http_exception(ErrorCodes.WJSC010)
  255. @staticmethod
  256. def invalid_parameters():
  257. logger.error(ErrorCodes.WJSC009)
  258. return create_http_exception(ErrorCodes.WJSC009)
  259. @staticmethod
  260. def internal_error(original_error: Exception):
  261. logger.error(ErrorCodes.WJSC011)
  262. return create_server_error("WJSC011", original_error)
  263. class LaunchReviewErrors:
  264. """启动审查接口错误"""
  265. @staticmethod
  266. def missing_parameters():
  267. logger.error(ErrorCodes.QDSC001)
  268. return create_http_exception(ErrorCodes.QDSC001)
  269. @staticmethod
  270. def invalid_param_format():
  271. logger.error(ErrorCodes.QDSC002)
  272. return create_http_exception(ErrorCodes.QDSC002)
  273. @staticmethod
  274. def unauthorized():
  275. logger.error(ErrorCodes.QDSC003)
  276. return create_http_exception(ErrorCodes.QDSC003)
  277. @staticmethod
  278. def invalid_user():
  279. logger.error(ErrorCodes.QDSC004)
  280. return create_http_exception(ErrorCodes.QDSC004)
  281. @staticmethod
  282. def task_not_found():
  283. logger.error(ErrorCodes.QDSC005)
  284. return create_http_exception(ErrorCodes.QDSC005)
  285. @staticmethod
  286. def task_already_exists():
  287. logger.error(ErrorCodes.QDSC006)
  288. return create_http_exception(ErrorCodes.QDSC006)
  289. @staticmethod
  290. def project_plan_type_invalid():
  291. logger.error(ErrorCodes.QDSC007)
  292. return create_http_exception(ErrorCodes.QDSC007)
  293. @staticmethod
  294. def enum_type_invalid():
  295. logger.error(ErrorCodes.QDSC008)
  296. return create_http_exception(ErrorCodes.QDSC008)
  297. @staticmethod
  298. def enum_type_cannot_be_null():
  299. logger.error(ErrorCodes.QDSC009)
  300. return create_http_exception(ErrorCodes.QDSC009)
  301. @staticmethod
  302. def file_info_not_found(original_error: Exception):
  303. logger.error(ErrorCodes.QDSC010)
  304. return create_server_error("QDSC010", original_error)
  305. @staticmethod
  306. def internal_error(original_error: Exception):
  307. logger.error(ErrorCodes.QDSC011)
  308. return create_server_error("QDSC011", original_error)
  309. class ReviewResultsErrors:
  310. """审查结果接口错误"""
  311. @staticmethod
  312. def invalid_type():
  313. logger.error(ErrorCodes.SCJG001)
  314. return create_http_exception(ErrorCodes.SCJG001)
  315. @staticmethod
  316. def missing_param_id():
  317. logger.error(ErrorCodes.SCJG002)
  318. return create_http_exception(ErrorCodes.SCJG002)
  319. @staticmethod
  320. def invalid_id_format():
  321. logger.error(ErrorCodes.SCJG003)
  322. return create_http_exception(ErrorCodes.SCJG003)
  323. @staticmethod
  324. def unauthorized():
  325. logger.error(ErrorCodes.SCJG004)
  326. return create_http_exception(ErrorCodes.SCJG004)
  327. @staticmethod
  328. def invalid_user():
  329. logger.error(ErrorCodes.SCJG005)
  330. return create_http_exception(ErrorCodes.SCJG005)
  331. @staticmethod
  332. def task_not_found():
  333. logger.error(ErrorCodes.SCJG006)
  334. return create_http_exception(ErrorCodes.SCJG006)
  335. @staticmethod
  336. def no_review_results():
  337. logger.error(ErrorCodes.SCJG007)
  338. return create_http_exception(ErrorCodes.SCJG007)
  339. @staticmethod
  340. def server_error(original_error: Exception):
  341. logger.error(ErrorCodes.SCJG008)
  342. return create_server_error("SCJG008", original_error)