-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Option to raise exception on error codes #491
Comments
Something related to this that I think would be really nice is a way for the user of the generated code to more easily inspect the HTTP status code and body on error. Currently, when the API returns, if it is Instead of returning @dataclass
class TResponse:
data: Optional[T]
status_code: int
raw_payload: Dict[str, Any] The I came across this while working on writing a small consumer for my API. The requests were failing, but I couldn't tell why without breaking out Wireshark and looking at the raw server HTTP response messages. |
That's what the I avoided returning the actual |
🤦 Totally missed those. Thanks for the heads up, you already have exactly what I was looking for. |
They're mentioned in the generated READMEs but it's not super obvious. If there's a better way we could call those out so they're more discoverable, please let me know or make a PR with an improvement. Clear documentation is supposed to be a feature of this project 😅 |
Another alternative is creating the client with some extra configuration, in this case a
This would be kind of similar to what OpenApi client generator does for other languages, for example Typescript:
This will allow also the creation of middlewares which can be useful too. I know this solution is not ideal either as having a clean client object is pretty nice, but if you are ok with it I can contribute with a PR adding this feature. |
After #775 raising exceptiosn for error codes can be done via event hooks—so I'm considering that as closing this. |
@dbanty One downside of that approach is that error responses often don't have response types in an openapi schema, so any endpoint that enumerates its error codes ends up with a return type of |
Closing this, will track built-in exceptions in #254 |
Discussed in #254
Originally posted by theFong November 28, 2020
Is your feature request related to a problem? Please describe.
In some cases, if an unexpected error code is returned (or even an expected error code), I would rather raise an exception than return
None
. This is because depending on the linter and adoption of type annotation in a code base I may not handle aNone
variable. In this case an exception raising status code, message or other meta data would be more useful to debug especially when using exception logging software like Sentry.Describe the solution you'd like
I see a couple of interface possibilities if this feature would make sense to implement.
Add new functions with a must prefix:
must_sync
must_sync_detailed
must_async
must_async_detailed
This is inspired by a Golang's pattern of prefixing "must" to functions which may panic. The main downside is the addition of 4 new methods.
Another solution would be to provide a
raise_for_status
method similar to python requests library on theResponse
object. This interface would allow the passing of args to specify raising in case of any error code or just unexpected ones. The side effect here is that like remembering to handle None you would have to remember toraise_for_status
. This side effect exists when using the requests library so is not a big deal.Describe alternatives you've considered
Currently in cases where I would like to fail hard, I have a wrapper function which takes a
..._detailed
function and builds an exception with meta data ifparsed
isNone
. This works fine.Interested in thoughts/discussion!
The text was updated successfully, but these errors were encountered: