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 去了哪里?
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)數據的要求。那我們能不能自定義 異常處理方法呢?答案當然是可以。
下面介紹兩種自定義異常處理的方法:
使用 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)
參考鏈接
最后,感謝女朋友支持。
歡迎關注(April_Louisa)
請我喝芬達