资讯专栏INFORMATION COLUMN

Python web 框架Sanic 学习: 自定义 Exception

2i18ns / 2272人阅读

摘要:是一个和类的基于的框架,它使用了异步特性,有远超的性能。那我们能不能自定义异常处理方法呢答案当然是可以。提供了的响应对象。以下自定义的异常处理类无效的使用捕获异常,返回自定义响应数据参考链接最后,感谢女朋友支持。

Sanic 是一个和类Flask 的基于Python3.5+的web框架,它使用了 Python3 异步特性,有远超 flask 的性能。

编写 RESTful API 的时候,我们会定义特定的异常错误类型,比如我定义的错误返回值格式为:

{
  "error_code": 0,
  "message": "string",
  "text": "string"
}

不同的错误信息指定不同的 http 状态码。

sanic 提供了几种常用的 exception:

NotFound(404)

Forbidden(403)

ServerError(500)

InvalidUsage(400)

Unauthorized(401)

RequestTimeout(408)

PayloadTooLarge(413)

这些 exception 继承自 SanicException 类:

class SanicException(Exception):

    def __init__(self, message, status_code=None):
        super().__init__(message)

        if status_code is not None:
            self.status_code = status_code

从上述代码可以看出,这些异常只能指定 message 和 status_code 参数,那我们可不可以自定义 exception 然后在自定义的 exception 中增加参数呢?下面的代码是按照这个思路修改后的代码:

class ApiException(SanicException):

    def __init__(self, code, message=None, text=None, status_code=None):
        super().__init__(message)
        self.error_code = code
        self.message = message
        self.text = text

        if status_code is not None:
            self.status_code = status_code

使用后我得到一个结果如下:

从结果可以发现,除了 http 状态码使我想要的其它全错,连 content-type 都是 text/plain; charset=utf-8,为什么会这样呢,我们定义的参数code 和 text 去了哪里?

翻开 sanic handler 的代码https://github.com/channelcat/sanic/blob/master/sanic/handlers.py我找到了答案:

def default(self, request, exception):
        self.log(format_exc())
        if issubclass(type(exception), SanicException):
            # 如果是 SanicException 类,返回格式是定义好的,
            # response 处理方法用的是 text
            return text(
                "Error: {}".format(exception),
                status=getattr(exception, "status_code", 500),
                headers=getattr(exception, "headers", dict())
            )
        elif self.debug:
            html_output = self._render_traceback_html(exception, request)

            response_message = (
                "Exception occurred while handling uri: "{}"
{}".format(
                    request.url, format_exc()))
            log.error(response_message)
            return html(html_output, status=500)
        else:
            return html(INTERNAL_SERVER_ERROR_HTML, status=500)

从源码可以看出,如果response 结果是 SanicException 类,response 处理方法会改用text,响应内容格式为 Error: status_code

看来直接使用自定义异常类的方法不能满足我们上边定义的 json 格式(需要有 error_code、message 和 text)数据的要求。那我们能不能自定义 异常处理方法呢?答案当然是可以。

下面介绍两种自定义异常处理的方法:

使用 response.json

这种方法比较简单,既然 sanic 异常处理是把错误信息使用 response.text() 方法返回,那我们改成 response.json() 不就可以了么。sanic response 提供了 json 的响应对象。可以使用 response.json 定义一个错误处理方法:

def json_error(error_code, message, text, status_code):
    return json(
        {
            "error_code": error_code,
            "message": message,
            "text": text
        },
        status=status_code)

这样我们只需要在需要抛出异常的地方 return json_error(code, msg, text, status_code)

使用这种方法有一点需要注意:

def get_account():
    ...
    if account:
        return account
    else:
        # 如果用户没找到 返回错误信息
        return json_error(code, msg, text, status_code)
    
@app.route("/")
async def test(request):
    account = get_account()
    return text("Hello world!")

这段代码中,如果我们没有找到用户信息,json_error 的返回结果会赋值给 account,并不会抛出异常,如果需要抛出异常,我们需要在 test 方法中检查 account 的结果,如果包含 account 是 response.json 对象, 直接 return, 更正后的代码如下:

@app.route("/")
async def test(request):
    account = get_account()
    if isinstance(account, response.json):
        return account
    return text("Hello world!")

这样虽然简单,但是会增加很多不必要的判断,那有没有方法可以直接抛出异常呢?这时就可以使用 sanic 提供的 @app.exception 装饰器了。

使用 Handling exceptions

sanic 提供了一个 @app.exception装饰器,使用它可以覆盖默认的异常处理方法。它的使用方法也很简单:

from sanic.response import text
from sanic.exceptions import NotFound

@app.exception(NotFound)
def ignore_404s(request, exception):
    return text("Yep, I totally found the page: {}".format(request.url))

这个装饰器允许我们传入一个需要捕获的异常的列表,然后,就可以在自定义方法中返回任意的响应数据了。

以下自定义的异常处理类:

error_codes = {
    "invalid_token": ("Invalid token", "无效的token"),
}

def add_status_code(code):
    """
    Decorator used for adding exceptions to _sanic_exceptions.
    """
    def class_decorator(cls):
        cls.status_code = code
        return cls
    return class_decorator


class MetisException(SanicException):

    def __init__(self, code, message=None, text=None, status_code=None):
        super().__init__(message)
        self.error_code = code
        _message, _text = error_codes.get(code, (None, None))
        self.message = message or _message
        self.text = text or _text

        if status_code is not None:
            self.status_code = status_code

@add_status_code(404)
class NotFound(MetisException):
    pass

@add_status_code(400)
class BadRequest(MetisException):
    pass

# 使用 app.exception 捕获异常,返回自定义响应数据
@app.exception(Unauthorized, NotFound, BadRequest)
def json_error(request, exception):
    return json(
        {
            "error_code": exception.error_code,
            "message": exception.message,
            "text": exception.text
        },
        status=exception.status_code)
参考链接

Sanic Exceptions

Metis


最后,感谢女朋友支持。

欢迎关注(April_Louisa) 请我喝芬达

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40690.html

相关文章

  • Sanic教程:快速开始

    摘要:快速开始在安装之前在支持异步的过程中,都经历了哪些比较重大的更新。踏出第一步我们将正式使用来构建一个项目,让我们踏出第一步,利用来编写一个返回字符串的服务程序。本次示例的源代码全部在上,见。 快速开始 在安装Sanic之前,让我们一起来看看Python在支持异步的过程中,都经历了哪些比较重大的更新。 首先是Python3.4版本引入了asyncio,这让Python有了支持异步IO的标...

    warmcheng 评论0 收藏0
  • python 最快 web 框架 Sanci 快速入门

    摘要:详细信息可以看下这个问题先在说下我的部署方式使用部署配置文件启动方式总结试用了下,把之前的一个聊天机器人从改成了。预告下一篇将介绍如何使用一步一步创建一个聊天机器人。 简介 Sanic 是一个和类Flask 的基于Python3.5+的web框架,它编写的代码速度特别快。除了像Flask 以外,Sanic 还支持以异步请求的方式处理请求。这意味着你可以使用新的 async/await ...

    snifes 评论0 收藏0
  • 基于python3.5+的web框架sanic中文入门教程

    摘要:简介是一款用写的,用法和类似,的特点是非常快官网速度比较框架实现基础每秒请求数平均时间安装环境创建文件,写入下面的内容运行是不是看起来和一样属性上传文件列表数据数据表单数据例子路由和差不多,一看就懂注册中间件异常处 简介 sanic是一款用python3.5+写的web framework,用法和flask类似,sanic的特点是非常快github官网:https://github.c...

    booster 评论0 收藏0
  • sanic异步框架之中文文档

    摘要:实例实例测试结果增加路由实例测试结果提供了一个方法,根据处理程序方法名生成。异常抛出异常要抛出异常,只需从异常模块中提出相应的异常。 typora-copy-images-to: ipic [TOC] 快速开始 在安装Sanic之前,让我们一起来看看Python在支持异步的过程中,都经历了哪些比较重大的更新。 首先是Python3.4版本引入了asyncio,这让Python有了支...

    elliott_hu 评论0 收藏0
  • 基于Sanic的微服务基础架构

    摘要:在中,官方的异步协程库正式成为标准。本项目就是以为基础搭建的微服务框架。使用做单元测试,并且使用来避免访问其他微服务。跟踪每一个请求,记录请求所经过的每一个微服务,以链条的方式串联起来,对分析微服务的性能瓶颈至关重要。 介绍 使用python做web开发面临的一个最大的问题就是性能,在解决C10K问题上显的有点吃力。有些异步框架Tornado、Twisted、Gevent 等就是为了解...

    seasonley 评论0 收藏0

发表评论

0条评论

2i18ns

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<