Connexion view function is throwing 500 error instead of catching the exception

524 views Asked by At

So, I have the following view function in connexion (I am trying to work on a Open APIV3 project):

def my_view_func_in_python():
    return_dict = {"status": False, "stdout": None, "stderr": None}
    try:
        payload = connexion.request.get_json()
        json_data = payload["json"]
        fileName = payload["fileName"]
        text_lines = []

        for line in json_data["res"][0]["lines"]:
            text_lines.append(line["text"])

        text = " ".join(text_lines)
        os.remove("./assets/request.jpg")

        return_dict.update([
            ("status", True)
        ])

    except BaseException as ex:
        return_dict.update([
            ("stderr", ex)
        ])

    return return_dict

Now for some reason, if I give an empty json field value via swagger, the code breaks in the line which has for loop for line in json_data["res"][0]["lines"]. But if it breaks, it should go in the except line and return the return_dict peacefully. But what it returns me is a 500 error on swagger.

SWAGGER OUTPUT:

{
  "detail": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
  "status": 500,
  "title": "Internal Server Error",
  "type": "about:blank"
}

and, what error it throws me in the backend where I have hosted the app:

TypeError: Object of type 'KeyError' is not JSON serializable

I have tried alot of places, open apiv3 specs, swagger documentation and connexion documentation. I am not able to just catch this error! What can I do?

0

There are 0 answers