代码之家  ›  专栏  ›  技术社区  ›  nad

在POST请求处理程序内的python flask应用程序中传递异常

  •  0
  • nad  · 技术社区  · 5 年前

    我有一个python flask应用程序,伪代码如下所示。

    @app.route('/predict', methods=['POST'])
        def transformation():
           try:
               var1 = function_1()
               # do something with var1
               var2 = function_2()
           except Exception as e:
               return jsonify({'message': '{}: {}'.format(type(e).__name__, e)}), 500
    

    正如你所看到的 POST 调用处理程序将通用异常消息返回给客户端。但是我想根据这些异常消息是否来自 function_1 function_2

    this thread 并理解以下操作是可能的-

    try:
     #something1
     #something2
    except ExceptionType1:
     #return xyz
    except ExceptionType2:
     #return abc
    

    但它怎么会知道呢 ExceptionType1 是从哪里来的 function_1() function_2() . 我应该如何从中传递异常 函数_1() 函数_2() 陷入困境 try-except

    0 回复  |  直到 5 年前