自定义python框架_Python web 框架Sanic 学习: 自定义 Exception
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)
请我喝芬达
自定义python框架_Python web 框架Sanic 学习: 自定义 Exception相关推荐
- 新浪云python示例_Python Web框架Flask中使用新浪SAE云存储实例
对于部署在新浪应用引擎SAE上的项目,使用新浪SAE云存储是不错的存储方案. 新浪SAE云存储仅能在SAE环境中正常使用,对它进行简单封装后,可以直接在Flask中使用,项目代码见GitHub上Fla ...
- python django框架_Python Web框架【Django框架第一篇基础】
{#把后端的变量嵌套进入前端页面#} Current time: {{ Time }}
- python四大主流web框架
python四大主流web框架 转载自博客:https://www.cnblogs.com/an-wen/p/11330834.html --爱文飞翔 Python 四大主流 Web 编程框架 目前P ...
- 手撸web框架即引入框架思想,wsgierf模块,动静态网页,模板语法jinja2,python三大主流web框架,django安装,三板斧...
手撸web框架 web框架 什么是web框架? 暂时可理解为服务端. 软件开发架构 C/S架构 B/S架构 # 后端 import socketserver = socket.socket() # 不 ...
- Python之简易Web框架搭建
Python之简易Web框架搭建 Web框架介绍 WSGI协议 Web框架开发 项目结构 MyWebServer.py 之前的静态服务器代码 WSGI协议的要求 更新代码 framework.py 返 ...
- asp.net web开发框架_用Python开发一个Web框架
一.Web框架 首先我们今天要做的事是开发一个Web框架.可能听到这你就会想.是不是很难啊?这东西自己能写出来? 如果你有这种疑惑的话,那就继续看下去吧.相信看完今天的内容你也能写出一个自己的Web框 ...
- 什么是Web框架、web框架有什么功能?
随着Web最新发展趋势的不断升级,Web项目开发也越来越难,而且需要花费更多的开发时间.所以,Web程序员灵活运用Web开发框架显得更为重要.下面为大家简单的介绍一下Web框架的基本内容,希望对大家有 ...
- python 服务框架_Python Web服务高并发框架【1】(Tornado)
本节学习目标: (1)了解Tornado的特点 (2)了解Tornado工作流程 (3)掌握Tornado在Window及Linux中的安装 (4)理解同步.异步 (5)协程基础编程 本节课程内容: ...
- python框架sanic_Python Web框架Sanic框架初识
Sanic 既是一个Python web 服务器,又是一个web框架,它为速度而生.Sanic只支持Python 3.5及其以上版本,允许我们使用 async/await 语法来使我们的代码非阻塞且快 ...
最新文章
- 在线作图|如何绘制一张山脊图
- TSP问题遗传算法通用Matlab程序
- Spring JTA multiple resource transactions in Tomcat with Atomikos example--转载
- 区块链即数字生产关系
- VTK:可视化之VectorText
- C# WPF MVVM开发框架Caliburn.Micro常用功能指南②
- USACO 6.1.3 Cow XOR
- mysql分页查询报错,及解决
- 卓越领导者的智慧(精华版)
- 采用HTML5搭建的多个网站尝鲜试用
- 【tyvj1520】 树的直径
- mac obs 录屏黑屏_有了它,我把其他录屏软件都卸载了!
- django-spaghetti-and-meatballs 0.2.0 : Python Package Index
- 迅雷java_Java实现迅雷地址转成普通地址
- 透彻理解cmake(含PRIVATE,PUBLIC,INTERFACE的详细解释)
- WinRAR 5.40无弹窗广告注册版下载
- spring boot面试问题集锦
- 简洁的表白java_java简短表白代码是什么
- P1262 间谍网络+Trajan 缩点(有向图)
- org.jboss.netty.internal.LoggerConfigurator.DESCRIBED is already registered
热门文章
- 英语口语week 14 Thursday
- 广州学车科目三路考操作步骤要领
- MPEG-2TS码流编辑的原理及其应用(转载
- Java中十六进制转换 Integer.toHexString()
- java.util.IdentityHashMap.entrySet()方法实例
- 时间,数字 ,字符串之间的转换
- sql:无法解决 equal to 操作中 Chinese_PRC_CI_AS 和 Chinese_Taiwan_Stroke_CI_AS 之间的排序规则冲突。...
- Python中的getpass模块
- Android - AsyncTask你知道多少?
- 奇怪的bug,不懂Atom在添加markdown-themeable-pdf,在配置好phantomjs的情况下报错