diff --git a/Utils/ErrorUtil.py b/Utils/ErrorUtil.py index 385e51b..d09d659 100644 --- a/Utils/ErrorUtil.py +++ b/Utils/ErrorUtil.py @@ -2,10 +2,57 @@ import time import traceback from flask import request +from werkzeug.exceptions import BadRequest from DBHelper.MongoHelper import MongoHelper +def get_req_detail(req, param_type): + """ + 获取请求参数信息 + 若异常请求则返回空字符串 + """ + try: + if param_type == 'args': + return req.args.__str__() + elif param_type == 'json': + return req.json.__str__() + else: + return '' + except BadRequest: + return '' + + +class JustThrowError(RuntimeError): + """自定义抛出异常信息""" + + def __init__(self, error_info): + self.error_info = error_info + + def __str__(self): + return self.error_info + + +class APIReturnError(RuntimeError): + """接口返回异常信息""" + def __init__(self, error_info, status_code): + self.error_info = error_info # 异常信息 + self.status_code = status_code # 状态码 + + def __str__(self): + return self.error_info + + +class AttrCheckError(RuntimeError): + """属性检查异常""" + + def __init__(self, error_info): + self.error_info = error_info + + def __str__(self): + return self.error_info + + class CheckFailed(RuntimeError): """检查异常""" @@ -25,8 +72,8 @@ class CheckFailed(RuntimeError): "path": request.path, "method": request.method, "headers": request.headers.__str__(), - "args": request.args.__str__(), - "json": request.json.__str__() + "args": get_req_detail(request, 'args'), + "json": get_req_detail(request, 'json') }, "traceback": traceback.format_exc(), "exception": type(self).__name__, @@ -42,7 +89,7 @@ class CheckFailed(RuntimeError): class LogConditionCheckFailed(CheckFailed): - """直接记录检查异""" + """直接记录检查异常""" def __init__(self, failed_info, status_code): self.failed_info = failed_info # 失败信息 @@ -51,4 +98,4 @@ class LogConditionCheckFailed(CheckFailed): class ReturnConditionCheckFailed(CheckFailed): - """条件检查失败抛出异常 接口返回失败原因和状态码""" + """条件检查失败 抛出异常 接口返回失败原因和状态码"""