返回Python和瓶API错误消息 [英] Returning API Error Messages with Python and Flask

查看:147
本文介绍了返回Python和瓶API错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计使用Python和瓶一个RESTful API。正如预期的那样,这个API需要接收一个API请求并返回数据,如果一切顺利,但在一个错误的情况下,它需要轻轻失败并返回正确的错误。我通常在一个错误的结果,但在这种情况下,我需要返回错误信息给用户抛出异常(try-catch块?)。

我目前正在处理错误的方法是有我的函数返回的数据和错误,并在每个级别来检查数据,最后返回数据或者错误的API函数的调用者。

这里的问题是,它可以得到笨重当有函数调用几个级别,要求我的函数来传送数据和错误几次,并执行检查每个时间

有没有更好的方法来做到这一点?什么是一些改进,我可以做,使误差传播更加简洁大方?

下面是我目前正在返回错误的方式的例子:

  DEF GET_DATA()
    数据1,没有显示错误= get_some_data()函数#
    如果DATA1是无:
         返回None无法检索数据1
    数据2,未显示错误= get_some_other_data()函数#
    如果数据2是无:
         返回None无法检索数据2
    回报(DATA1,DATA2),无@ app.route(/ API /法,方法= ['GET'])
DEF()方法:
    数据错误= GET_DATA()
    如果数据无:
        如果错误是无:
            错误=未知错误
        返回json.dumps({错误:错误}),500
    返回json.dumps({数据:数据}),200


解决方案

您可以使用 中止(HTTP_ code) 返回一个适当的HTTP code到客户端,或只是提出一个非HTTP例外。并使用 @ app.errorhandler()装饰提供HTTP错误和任意异常的自定义处理程序。你也可以使用一个普通的try / except块,你准备好处理异常。蟒蛇是不是去你能使用异常。

I am designing a RESTful API using Python and Flask. As expected, the API needs to receive an API request and return data if all goes well, but in the instance of an error, it needs to fail softly and return the proper error. I typically raise exceptions when an error results, but in this case I need to return the error message to the user (try-catch block?).

The way I am currently dealing with errors is to have my functions return both the data and an error, and to check for the data at each level, finally returning either the data or the error to the caller of the API function.

The problem with this is that it can get cumbersome when there are several levels of function calls, requiring my functions to pass data and errors several times and perform checks each time.

Is there a better way to do this? What are some improvements I could make to make the error propagation more simple and elegant?

Here is an example of the way I'm currently returning errors:

def get_data()
    data1, error = get_some_data() # function not shown
    if data1 is None:
         return None, "could not retrieve data1"
    data2, error = get_some_other_data() # function not shown
    if data2 is None:
         return None, "could not retrieve data2"
    return (data1, data2), None

@app.route("/api/method", methods=['GET'])
def method():
    data, error = get_data()
    if data is None:
        if error is None:
            error = "unknown error"
        return json.dumps({ "error": error }), 500
    return json.dumps({ "data": data }), 200

解决方案

You could use abort(http_code) to return an appropriate http code to the client or just raise a non-http exception. And use @app.errorhandler() decorator to provide a custom handler for http errors and arbitrary exceptions. You could also use an ordinary try/except block where you are ready to handle an exception. Python is not Go you can use exceptions.

这篇关于返回Python和瓶API错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆