推荐学习书目
› 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 · 2204 views
    This topic created in 3431 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
    wentian
    Jun 13, 2017
    1
    封装 Result 是最好的

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

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

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