error_schemas.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. QDSC012 = {
  133. "code": "QDSC012",
  134. "error_type": "TASK_NOT_FOUND_OR_EXPIRED",
  135. "message": "任务ID不存在或已过期,请重新检查callback_task_id是否正确,或重新上传文件",
  136. "status_code": 404
  137. }
  138. QDSC013 = {
  139. "code": "QDSC013",
  140. "error_type": "FILE_INFO_NOT_FOUND",
  141. "message": "文件信息获取失败,任务ID不存在或已过期",
  142. "status_code": 404
  143. }
  144. # 审查结果接口错误码 (SCJG001-SCJG008)
  145. SCJG001 = {
  146. "code": "SCJG001",
  147. "error_type": "INVALID_TYPE",
  148. "message": "结果类型无效(非'summary'或'issues')",
  149. "status_code": 400
  150. }
  151. SCJG002 = {
  152. "code": "SCJG002",
  153. "error_type": "MISSING_PARAM_ID",
  154. "message": "callback_task_id缺失",
  155. "status_code": 400
  156. }
  157. SCJG003 = {
  158. "code": "SCJG003",
  159. "error_type": "INVALID_ID_FORMAT",
  160. "message": "callback_task_id格式错误",
  161. "status_code": 400
  162. }
  163. SCJG004 = {
  164. "code": "SCJG004",
  165. "error_type": "UNAUTHORIZED",
  166. "message": "认证失败(未提供或无效的Authorization)",
  167. "status_code": 401
  168. }
  169. SCJG005 = {
  170. "code": "SCJG005",
  171. "error_type": "INVALID_USER",
  172. "message": "用户标识未提供或无效",
  173. "status_code": 403
  174. }
  175. SCJG006 = {
  176. "code": "SCJG006",
  177. "error_type": "TASK_NOT_FOUND",
  178. "message": "callback_task_id不存在或已过期",
  179. "status_code": 404
  180. }
  181. SCJG007 = {
  182. "code": "SCJG007",
  183. "error_type": "NO_REVIEW_RESULTS",
  184. "message": "无审查结果数据",
  185. "status_code": 404
  186. }
  187. SCJG008 = {
  188. "code": "SCJG008",
  189. "error_type": "SERVER_ERROR",
  190. "message": "服务端内部错误(审查结果生成失败)",
  191. "status_code": 500
  192. }
  193. def create_http_exception(error_code: Dict[str, Any], custom_message: str = None) -> HTTPException:
  194. """
  195. 创建HTTP异常
  196. Args:
  197. error_code: 错误码字典
  198. custom_message: 自定义错误消息,可选
  199. Returns:
  200. HTTPException: FastAPI异常对象
  201. """
  202. detail = {
  203. "code": error_code["code"],
  204. "error_type": error_code["error_type"],
  205. "message": custom_message or error_code["message"]
  206. }
  207. return HTTPException(
  208. status_code=error_code["status_code"],
  209. detail=detail
  210. )
  211. def create_server_error(error_code: str, original_error: Exception) -> HTTPException:
  212. """
  213. 创建服务器内部错误异常
  214. Args:
  215. error_code: 错误码 (如 "WJSC008", "QDSC006", "SCJG008")
  216. original_error: 原始异常
  217. Returns:
  218. HTTPException: FastAPI异常对象
  219. """
  220. error_map = {
  221. "WJSC011": ErrorCodes.WJSC011,
  222. "QDSC006": ErrorCodes.QDSC006,
  223. "SCJG008": ErrorCodes.SCJG008
  224. }
  225. error_config = error_map.get(error_code, ErrorCodes.WJSC008)
  226. message = f"{error_config['message']}: {str(original_error)}"
  227. return create_http_exception(error_config, message)
  228. # 便捷的错误创建函数
  229. class FileUploadErrors:
  230. """文件上传接口错误"""
  231. @staticmethod
  232. def file_missing():
  233. logger.error(ErrorCodes.WJSC001)
  234. return create_http_exception(ErrorCodes.WJSC001)
  235. @staticmethod
  236. def file_multiple():
  237. logger.error(ErrorCodes.WJSC002)
  238. return create_http_exception(ErrorCodes.WJSC002)
  239. @staticmethod
  240. def file_rejected(message: str = None):
  241. logger.error(ErrorCodes.WJSC003)
  242. return create_http_exception(ErrorCodes.WJSC003, message)
  243. @staticmethod
  244. def file_format_unsupported():
  245. logger.error(ErrorCodes.WJSC004)
  246. return create_http_exception(ErrorCodes.WJSC004)
  247. @staticmethod
  248. def file_size_exceeded():
  249. logger.error(ErrorCodes.WJSC005)
  250. return create_http_exception(ErrorCodes.WJSC005)
  251. @staticmethod
  252. def project_plan_type_invalid():
  253. logger.error(ErrorCodes.WJSC006)
  254. return create_http_exception(ErrorCodes.WJSC006)
  255. @staticmethod
  256. def unauthorized():
  257. logger.error(ErrorCodes.WJSC007)
  258. return create_http_exception(ErrorCodes.WJSC007)
  259. @staticmethod
  260. def invalid_user():
  261. logger.error(ErrorCodes.WJSC008)
  262. return create_http_exception(ErrorCodes.WJSC008)
  263. @staticmethod
  264. def task_already_exists():
  265. logger.error(ErrorCodes.WJSC010)
  266. return create_http_exception(ErrorCodes.WJSC010)
  267. @staticmethod
  268. def invalid_parameters():
  269. logger.error(ErrorCodes.WJSC009)
  270. return create_http_exception(ErrorCodes.WJSC009)
  271. @staticmethod
  272. def internal_error(original_error: Exception):
  273. logger.error(ErrorCodes.WJSC011)
  274. return create_server_error("WJSC011", original_error)
  275. class LaunchReviewErrors:
  276. """启动审查接口错误"""
  277. @staticmethod
  278. def missing_parameters():
  279. logger.error(ErrorCodes.QDSC001)
  280. return create_http_exception(ErrorCodes.QDSC001)
  281. @staticmethod
  282. def invalid_param_format():
  283. logger.error(ErrorCodes.QDSC002)
  284. return create_http_exception(ErrorCodes.QDSC002)
  285. @staticmethod
  286. def unauthorized():
  287. logger.error(ErrorCodes.QDSC003)
  288. return create_http_exception(ErrorCodes.QDSC003)
  289. @staticmethod
  290. def invalid_user():
  291. logger.error(ErrorCodes.QDSC004)
  292. return create_http_exception(ErrorCodes.QDSC004)
  293. @staticmethod
  294. def task_not_found():
  295. logger.error(ErrorCodes.QDSC005)
  296. return create_http_exception(ErrorCodes.QDSC005)
  297. @staticmethod
  298. def task_already_exists():
  299. logger.error(ErrorCodes.QDSC006)
  300. return create_http_exception(ErrorCodes.QDSC006)
  301. @staticmethod
  302. def project_plan_type_invalid():
  303. logger.error(ErrorCodes.QDSC007)
  304. return create_http_exception(ErrorCodes.QDSC007)
  305. @staticmethod
  306. def enum_type_invalid():
  307. logger.error(ErrorCodes.QDSC008)
  308. return create_http_exception(ErrorCodes.QDSC008)
  309. @staticmethod
  310. def enum_type_cannot_be_null():
  311. logger.error(ErrorCodes.QDSC009)
  312. return create_http_exception(ErrorCodes.QDSC009)
  313. @staticmethod
  314. def file_info_not_found(original_error: Exception):
  315. logger.error(ErrorCodes.QDSC010)
  316. return create_server_error("QDSC010", original_error)
  317. @staticmethod
  318. def internal_error(original_error: Exception):
  319. logger.error(ErrorCodes.QDSC011)
  320. return create_server_error("QDSC011", original_error)
  321. @staticmethod
  322. def task_not_found_or_expired():
  323. logger.error(ErrorCodes.QDSC012)
  324. return create_http_exception(ErrorCodes.QDSC012)
  325. @staticmethod
  326. def file_info_not_found():
  327. logger.error(ErrorCodes.QDSC013)
  328. return create_http_exception(ErrorCodes.QDSC013)
  329. class ReviewResultsErrors:
  330. """审查结果接口错误"""
  331. @staticmethod
  332. def invalid_type():
  333. logger.error(ErrorCodes.SCJG001)
  334. return create_http_exception(ErrorCodes.SCJG001)
  335. @staticmethod
  336. def missing_param_id():
  337. logger.error(ErrorCodes.SCJG002)
  338. return create_http_exception(ErrorCodes.SCJG002)
  339. @staticmethod
  340. def invalid_id_format():
  341. logger.error(ErrorCodes.SCJG003)
  342. return create_http_exception(ErrorCodes.SCJG003)
  343. @staticmethod
  344. def unauthorized():
  345. logger.error(ErrorCodes.SCJG004)
  346. return create_http_exception(ErrorCodes.SCJG004)
  347. @staticmethod
  348. def invalid_user():
  349. logger.error(ErrorCodes.SCJG005)
  350. return create_http_exception(ErrorCodes.SCJG005)
  351. @staticmethod
  352. def task_not_found():
  353. logger.error(ErrorCodes.SCJG006)
  354. return create_http_exception(ErrorCodes.SCJG006)
  355. @staticmethod
  356. def no_review_results():
  357. logger.error(ErrorCodes.SCJG007)
  358. return create_http_exception(ErrorCodes.SCJG007)
  359. @staticmethod
  360. def server_error(original_error: Exception):
  361. logger.error(ErrorCodes.SCJG008)
  362. return create_server_error("SCJG008", original_error)