Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise specifically typed exceptions when a request fails #16

Open
nathanfdunn opened this issue Aug 14, 2022 · 2 comments
Open

Raise specifically typed exceptions when a request fails #16

nathanfdunn opened this issue Aug 14, 2022 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@nathanfdunn
Copy link

Is your feature request related to a problem? Please describe.
When a request to Dalle fails, the reason is logged but None is returned. This makes it difficult to programmatically figure out why the request failed and respond appropriately. For instance, if a request fails because the prompt violates safety standards, you may want to tell the user to try a different prompts. But if a request fails because of a lack of credits, you may want to tell the user to go add more credits before they try again.

Describe the solution you'd like
I think it would be helpful for the Dalle2 client to raise exceptions when there are problems so code like the following would work

import dalle2

try:
	images = dalle2.Dalle2('sess-XXXX').generate('prompt')
except: dalle2.exceptions.PromptRejectedException:
	return {'message': 'Try a different prompt'}
except: dalle2.exceptions.OutOfCreditsException:
	return {'message': 'Please refill your credits'}

Describe alternatives you've considered
This would be a breaking change, so it should be considered carefully. Here are some alternatives that would preserve backwards compatibility:

Provide a context object dalle2.error_status that would provide information about the most recent request (similar to how Flask provides a request context object https://flask.palletsprojects.com/en/1.1.x/reqcontext/)

import dalle2

images = dalle2.Dalle2('sess-XXXX').generate('prompt')
if images is None:
	if dalle2.error_status.reason == dalle2.ErrorStatusReason.PROMPT_REJECTED:
		print('Prompt Rejected:', dalle2.error_status.message)
	elif dalle2.error_status.reason == dalle2.ErrorStatusReason.OUT_OF_CREDITS:
		...
	else:
		...

But this is less clean and more error prone than exceptions.

Have exceptions be opt-in like client = Dalle2('sess-XXXX', raise_on_error=True). But adding configuration overhead like this would make the package code more complex and also could cause a fair amount of friction for users.

@github-actions
Copy link

Thanks you for your first issue in dalle2-in-python

@ezzcodeezzlife
Copy link
Owner

yeah great idea. do you want to tackle this issue? seems like you have some experience with this. could assign you @nathanfdunn

@ezzcodeezzlife ezzcodeezzlife added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Aug 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants