From 7f78e584883994c7b8bb12d0d8ec88289074def7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 12 Mar 2024 13:35:30 -0700 Subject: [PATCH] refac: image generation error handling --- backend/apps/images/main.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/apps/images/main.py b/backend/apps/images/main.py index 31bfc0f5..e14b0f6a 100644 --- a/backend/apps/images/main.py +++ b/backend/apps/images/main.py @@ -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))