I had written a piece of code where I'm using traceback to return the exception stacktrace. But I forgot to import traceback.
Filename - trial_main.py
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class RequestJson(BaseModel):
data: str
@app.post("/trial", status_code=200)
async def trial(req:RequestJson):
try:
status = "default"
resp = {
"status": status,
"reason": "default reason"
}
# error point
a = 10/0
status = "complete"
reason = "Finished converting from base64 to image"
resp = {
"status": status,
"reason": reason
}
except Exception as e:
status = "incomplete"
resp = {
"status": status,
"reason": traceback.format_exc(),
}
finally:
return resp
if __name__ == '__main__':
uvicorn.run("trial_main:app", host="0.0.0.0", port=5001, log_level="info")
What's confusing me is why isn't the code exiting with an exception since traceback module is not found. Rather it returns the default response that was set earlier.
This is the API response that I'm getting -
{
"status": "default",
"reason": "default reason"
}
And this is the output on the terminal where I run the uvicorn server -
INFO: Started server process [14744]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:5001 (Press CTRL+C to quit)
INFO: 127.0.0.1:61809 - "POST /trial HTTP/1.1" 200 OK
Api endpoint -
[POST] http://127.0.0.1:5001/trial
Input to re-create the situation -
{"data": "randomString"}