flask后端实践02-全局Response返回和异常处理

全局Response返回和异常处理

github

前言

主要谈在Python中,

  1. 如何进行统一Response处理。
  2. 如何进行全局异常处理。

定义全局Response

BizResponse

根据Flask所需参数,定义status,message,header

返回的header,我们很少关心

返回的status,按照业务返回即可。一般成功返回200。错误根据服务端错误和客户端错误自行确定,项目初期,可以区分下即可。

返回的message 即我们接口的返回体,一般返回Json类型格式

这里是我们重点关心的地方,要保证有统一的返回结构体。

代码详细请看

app/model/biz_response.py

思路

我们定义统一的biz_response来处理Flask的所有返回参数。

其中,对于参数message 要求必须实现dict_msg()这个函数

dict_msg()函数负责将出参映射成为希望看到的样子

定义装饰器

装饰器的感觉和java的注解类似,实现 dict_response

python 复制代码
def dict_response(cls):
    def dict_msg(cls):
        return {key: value for key, value in cls.__dict__.items()}

    cls.dict_msg = dict_msg
    return cls

@dataclass
@dict_response
class TestResponse:
    name: str
    age: int

通过装饰器dict_response, 为TestResponse增加一个dict_msg的方法。

上述的方法也可以手动实现

python 复制代码
class TestResponse:
    name: str
    age: int
    def dict_msg(cls):
      return {"name":cls.name, "age":cls.age}

额外学习:鸭子类型

python没有Java中的interface的概念。

python本身是支持鸭子类型的。和go中鸭子类型一样。所以不需要像java中定义interface。

python 复制代码
class Animal:
    def bark(cls):
        print("im a animal")

class Dog:
    def bark(cls):
        print("wang wang")

animal : Animal = Dog()
animal.bark() # wang wang
python3 复制代码
#biz_response.py
class BizResponse:
    def dict_msg(cls):
        return {
            "Response": cls.message.dict_msg()
        }

@dataclass
class TestResponse:
    id: str
    
    def dict_msg(cls):
        # 手动改为大写开头的格式
        return {
            "Id": cls.id,
        }

所以在BizResponse中,直接使用message的dict_msg()方法,不用关心具体的类型。

同时这也意味着我们所有的返回Response,都应该实现这个方法。
详细示例请看

到此为止,我们就能完全统一我们的返回结构体。

使用单元测试

使用unittest进行单元测试

https://docs.python.org/zh-cn/3/library/unittest.html

使用go test 的类似写法,使用xxx_test 进行命名。

定义业务异常

代码详细请查看 app/common/error.py

python 复制代码
#app/common/error.py
class ErrorCode(Enum):
    InternalError = "InternalError"
    InvalidParameter = "InvalidParameter"


class BizException(Exception):
    def __init__(cls, code: ErrorCode, message: str):
        cls.code = code
        cls.message = message


class Error:
    def __init__(cls, msg: str, code: str):
        cls.message = msg
        cls.code = code

    def dict_msg(cls):
        return {
            "Error": {
                "Message": cls.message,
                "Code": cls.code
            }
        }

定义业务异常的目的也是为了更好地通过错误代码错误,并判断错误原因。

个人倾向于使用string定义错误码。方便可读性。

定义全局异常处理

python 复制代码
@app.errorhandler(Exception)
def error_handler(e):
    """
    全局异常捕获
    """
    logger.error("error=%s", e)
    logger.error("traceback=%s", traceback.format_exc())
    if isinstance(e, BizException):
        message = e.message
        code = e.code.value
    else:
        message = f"error={e}"
        code = ErrorCode.InternalError.value
    response = BizResponse.fail(Error(message, code))
    return jsonify(response.dict_msg()), response.status, response.header
相关推荐
卷无止境1 天前
C# 与 .NET 中的委托:把方法装进变量里
后端
绛洞花主敏明1 天前
Go操作xorm中间表多对多关联实战
开发语言·后端·golang
长栎1 天前
手写一个表达式计算器,你就理解解释器模式了
后端
长栎1 天前
foreach 语法糖背后,迭代器模式做了多少脏活
后端
狐言乱雨1 天前
深入理解KV Cache:大模型推理加速的核心技术
python
HLAIA光子1 天前
LLM缓存机制:你的API账单可以砍掉75%
后端·llm·ai编程
卷无止境1 天前
统计质量控制(SQC / SPC):用数据说话的质量哲学
后端
XovH1 天前
第 44篇 k8s之实战:将 Web 应用迁移到 Kubernetes(上)
后端
晓杰'1 天前
从0到1实现Balatro游戏后端(7):Boss Blind与特殊规则实现
后端·websocket·typescript·node.js·游戏开发·项目实战·nestjs
MariaH1 天前
Node.js 架构理解
后端