python 自定义错误返回JSON格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 自定义错误返回JSON格式相关的知识,希望对你有一定的参考价值。

def make_error(status_code, sub_code, message, action):
    response = jsonify({
        'status': status_code,
        'sub_code': sub_code,
        'message': message,
        'action': action
    })
    response.status_code = status_code
    return response


@route('/')
def my_view_function():
    # ...
    if need_to_return_error:
        return make_error(500, 42, 'You idiots!...', 'redirect...')

以上是关于python 自定义错误返回JSON格式的主要内容,如果未能解决你的问题,请参考以下文章

请求springboot接口的路径不存在,如何自定义status code和返回的JSON格式

请求springboot接口的路径不存在,如何自定义status code和返回的JSON格式

Ninja Framework 以自定义格式返回 JSON

如何以自定义 json 格式返回数据?

自定义统一api返回json格式(app后台框架搭建三)

Laravel-自定义API返回的JSON格式