I’m trying to catch errors in python discord requests to Dall-E 3.
The trouble is, I’m not super comfortable in Python, so its entirely possible I’m one misunderstanding away from having this fully working.
What I would like, ideally, is if the image generation fails, my code to send error information back as ‘response’ (that’s the variable that gets printed in Discord).
I’ve tried like a dozen different variants of this code, but it all just falls right through the except blocks.
Currently, either the image generates successfully, leading to:
- response = “”
- embed = a discord embed object with the generated image in it
These work great
Or the generation fails, because one of my friends put in something rude or whatever, the code falls right through all the “except” blocks and returns a blank response and a None embed. Why?
Code:
from openai Import OpenAI
ai_client = OpenAI()
#other stuff
def DallE(prompt):
embed = None
createdImage = None
response = ''
try :
createdImage = ai_client.images.generate(
model="dall-e-3",
prompt=prompt,
n=1,
size="1024x1024"
)
embed = discord.Embed(description=prompt.strip())
embed.set_image(url=createdImage.data[0].url)
except openai.BadRequestError as e:
print(f"BadRequestError: {e}")
response = 'error'
if e.response:
response = "e.status: " + e.response.status
else:
response = "e.message: " + e.message
pass
except:
print("Fall-through Exception")
response = 'Unexpected error, sorry.'
pass
finally:
return embed,response
# other stuff
The error I’m trying to catch and print to discord is this, although I’d like to ideally catch ALL errors and print them to discord:
openai.BadRequestError: Error code: 400 - {'error': {'code': 'content_policy_violation', 'message': 'Your request was rejected as a result of our safety system. Your prompt may contain text that is not allowed by our safety system.', 'param': None, 'type': 'invalid_request_error'}}