error_schemas.py 12 KB

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