Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Flask app not returning response when raising error (python)

$
0
0

I am building a Flask API in Python. When a function inside the API throws an error, the server does not return any response. I have tried using try-except blocks and print statements to debug the issue, but I am still unable to resolve it.

Here is some relevant code:

import jsonfrom flask import Flask, Response, request, jsonifydef first_function():    try:        second_function()    except Exception as error:        error_message = str(error).split("\n")[0]        raise ValueError(error_message)def second_function():    try:        print("second_function")        third_function()        print("I do not want to see this sentence.")    except Exception as error:        error_message = str(error).split("\n")[0]        raise ValueError(error_message)def third_function():    try:        print("third_function")        print(1/0)    except Exception as error:        error_message = str(error).split("\n")[0]        raise ValueError(error_message)@app.route('/control', methods=['POST'])def get():    response = ''    try:        first_function()        return jsonify(response)    except Exception as error:        error_message = str(error).split("\n")[0]        response =  {"text" : "Something went wrong when operating 'get' process. -> " + str(error_message)}        print("Error on main function ", error_message)        return jsonify(response)

Thank you for your help!


Viewing all articles
Browse latest Browse all 13891

Trending Articles