Sanic
是一个和类Flask 的基于Python3.5+的web框架,它使用了 Python3 异步特性,有远超 flask 的性能。html
编写 RESTful API 的时候,咱们会定义特定的异常错误类型,好比我定义的错误返回值格式为:python
{
"error_code": 0,
"message": "string",
"text": "string"
}复制代码
不一样的错误信息指定不一样的 http 状态码。git
sanic 提供了几种经常使用的 exception:github
这些 exception 继承自 SanicException 类:web
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 中增长参数呢?下面的代码是按照这个思路修改后的代码:json
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复制代码
使用后我获得一个结果以下:flask
从结果能够发现,除了 http 状态码使我想要的其它全错,连 content-type
都是 text/plain; charset=utf-8
,为何会这样呢,咱们定义的参数code 和 text 去了哪里?app
翻开 sanic handler 的代码github.com/channelcat/…我找到了答案:框架
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: "{}"\n{}'.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)数据的要求。那咱们能不能自定义 异常处理方法呢?答案固然是能够。
下面介绍两种自定义异常处理的方法:
这种方法比较简单,既然 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
装饰器了。
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)复制代码
最后,感谢女友支持。
欢迎关注(April_Louisa) | 请我喝芬达 |
---|---|
![]() |
![]() |