推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
keysona
V2EX  ›  Python

tornado 使用抛异常来返回 json 错误响应

  •  
  •   keysona · Jun 13, 2017 · 1885 views
    This topic created in 3267 days ago, the information mentioned may be changed or developed.
    最近在写 api 时,对错误响应的感觉处理很别扭。

    是 True / False

    还是封装一个 result 类。

    纠结了会,发现,异常好像很好用。
    5 replies    2017-06-14 18:42:02 +08:00
    wentian
        1
    wentian  
       Jun 13, 2017
    封装 Result 是最好的

    {
    "err_code":
    "err_msg":
    "detail"
    }
    LeeSeoung
        2
    LeeSeoung  
       Jun 13, 2017
    {
    "success":true
    "data":
    "message":error_msg
    }
    swulling
        3
    swulling  
       Jun 13, 2017
    我推荐是两者组合。

    如果正常返回的结果直接就是 data 本身
    如果是 4XX 或者 5XX,除了错误码之外,定义一个异常的 json 即可,一般是"success": true/fasle,"message" : "xxxx"

    tornado 可以自定义 errorhandler 来做到这个
    honmaple
        4
    honmaple  
       Jun 13, 2017
    {
    "status":"",
    "message":"",
    "description":"",
    "data":{}
    }
    我比较喜欢这样的,status 自定义状态码,200 为 success,message 返回相关信息
    TimePPT
        5
    TimePPT  
    PRO
       Jun 14, 2017
    status code + message
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2847 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 466ea39e · 45ms · UTC 06:03 · PVG 14:03 · LAX 23:03 · JFK 02:03
    ♥ Do have faith in what you're doing.