Sanic exception() 方法/函数

Sanic教程 2019-06-04 17:15:44 阅读(3391) 评论(0)

Sanic 类的exception()方法的API接口。

Sanic exception() 方法函数

exception() 方法/函数

定义

exception(*exceptions)

装饰要注册为异常处理程序的函数.

参数

  • exceptions:异常

返回值
被修饰的函数

例子

import asyncio
from sanic import Sanic
from sanic import response
from sanic.config import Config
from sanic.exceptions import RequestTimeout

Config.REQUEST_TIMEOUT = 1
app = Sanic(__name__)


@app.route('/')
async def test(request):
    await asyncio.sleep(3)
    return response.text('Hello, world!')


@app.exception(RequestTimeout)
def timeout(request, exception):
    return response.text('RequestTimeout from error_handler.', 408)

app.run(host='0.0.0.0', port=8000)

猿人学banner宣传图

我的公众号:猿人学 Python 上会分享更多心得体会,敬请关注。

***版权申明:若没有特殊说明,文章皆是猿人学 yuanrenxue.con 原创,没有猿人学授权,请勿以任何形式转载。***

说点什么吧...