zulip / python-zulip-api

Python library for the Zulip API.
https://zulip.com/api/
Apache License 2.0
350 stars 352 forks source link

zulip: Reraise exceptions in do_api_query. #735

Closed eeshangarg closed 2 years ago

eeshangarg commented 2 years ago

There are cases where the call to get_server_settings returns a JSON response with a connection error. In the case of such an unsuccessful response, we should raise an appropriate exception instead of parsing the response as though it was successful.

@timabbott Could you please take a look at this? Not sure if this is the right fix. Also, wondering if I should put a test for this somewhere in zulip_botserver?

timabbott commented 2 years ago

This looks correct to me. @andersk FYI.

For testing, I would prefer a test for the main zulip module, since the code is for that module. I'm not sure we are setup to make it easy to write such a test, but at the very least I'd like to see the output from trying to connect to a non Zulip server with this.

timabbott commented 2 years ago

@neiljp I was realizing it'd be helpful to have your review on this, since zulip-terminal probably cares about this aspect of error handling.

eeshangarg commented 2 years ago

@andersk I just addressed your feedback. Thanks!

@timabbott I am honestly not sure how to write a test for this within the module. Seems like it might be better to write a test for this server-side? Should we maybe write some sort of a mocking framework that mimics a Zulip server and raises exceptions at certain points?

timabbott commented 2 years ago

A server-side test that we run alongside test-api in CI could work. If we want to write proper tests, we need to write a bit of a test framework, which is probably out of scope for this issue.

Grepping for connection-error call_on_each_event makes use of the old error handling convention, and needs to be ported to handle exceptions. We can test the ported version manually by just pointing a bot at the development environment and trying things like ctrl+C on the development server to generate connection errors.

(It's an important part of the call_on_each_event interface that it will continue running even if you get connection errors once it has started, so that your bot using that API will continue working even if the server has temporary downtime)

eeshangarg commented 2 years ago

@timabbott This PR is mostly ready for review, just needs to be tested manually. Thanks!

eeshangarg commented 2 years ago

A summary of next steps or what needs to be done:

Thanks!

eeshangarg commented 2 years ago

Forgot to mention that the previous feedback has all been resolved on thsi PR! :)

timabbott commented 2 years ago

Merged, thanks @eeshangarg for your work on this.