refac: image generation error handling

This commit is contained in:
Timothy J. Baek 2024-03-12 13:35:30 -07:00
parent 26a187f5ac
commit 7f78e58488

View file

@ -293,6 +293,7 @@ def generate_image(
"size": form_data.size if form_data.size else app.state.IMAGE_SIZE,
"response_format": "b64_json",
}
r = requests.post(
url=f"https://api.openai.com/v1/images/generations",
json=data,
@ -300,7 +301,6 @@ def generate_image(
)
r.raise_for_status()
res = r.json()
images = []
@ -356,7 +356,10 @@ def generate_image(
return images
except Exception as e:
print(e)
if r:
print(r.json())
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e))
error = e
if r != None:
data = r.json()
if "error" in data:
error = data["error"]["message"]
raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(error))