python: Catch specific exceptions from requests.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2020-10-08 18:32:34 -07:00
committed by Tim Abbott
parent 17ac17286c
commit 7f69c1d3d5
4 changed files with 12 additions and 12 deletions

View File

@@ -46,7 +46,7 @@ def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) ->
# We could get a 501 error if the reverse proxy is up but the Django app isn't. # We could get a 501 error if the reverse proxy is up but the Django app isn't.
# Note that zulipdev.com is mapped via DNS to 127.0.0.1. # Note that zulipdev.com is mapped via DNS to 127.0.0.1.
return requests.get('http://zulipdev.com:9981/accounts/home').status_code == 200 return requests.get('http://zulipdev.com:9981/accounts/home').status_code == 200
except Exception: except requests.RequestException:
return False return False
@contextmanager @contextmanager

View File

@@ -449,7 +449,7 @@ def fetch_open_graph_image(url: str) -> Optional[Dict[str, Any]]:
# TODO: What if response content is huge? Should we get headers first? # TODO: What if response content is huge? Should we get headers first?
try: try:
content = requests.get(url, timeout=1).text content = requests.get(url, timeout=1).text
except Exception: except requests.RequestException:
return None return None
# Extract the head and meta tags # Extract the head and meta tags
# All meta tags are self closing, have no children or are closed # All meta tags are self closing, have no children or are closed

View File

@@ -68,14 +68,14 @@ class Command(ZulipBaseCommand):
registration_url = settings.PUSH_NOTIFICATION_BOUNCER_URL + "/api/v1/remotes/server/register" registration_url = settings.PUSH_NOTIFICATION_BOUNCER_URL + "/api/v1/remotes/server/register"
try: try:
response = requests.post(registration_url, params=request) response = requests.post(registration_url, params=request)
except Exception: except requests.RequestException:
raise CommandError( raise CommandError(
"Network error connecting to push notifications service " "Network error connecting to push notifications service "
f"({settings.PUSH_NOTIFICATION_BOUNCER_URL})", f"({settings.PUSH_NOTIFICATION_BOUNCER_URL})",
) )
try: try:
response.raise_for_status() response.raise_for_status()
except Exception: except requests.HTTPError:
content_dict = json.loads(response.content.decode("utf-8")) content_dict = json.loads(response.content.decode("utf-8"))
raise CommandError("Error: " + content_dict['msg']) raise CommandError("Error: " + content_dict['msg'])

View File

@@ -190,6 +190,7 @@ def join_bigbluebutton(request: HttpRequest, meeting_id: str = REQ(validator=che
if settings.BIG_BLUE_BUTTON_URL is None or settings.BIG_BLUE_BUTTON_SECRET is None: if settings.BIG_BLUE_BUTTON_URL is None or settings.BIG_BLUE_BUTTON_SECRET is None:
return json_error(_("Big Blue Button is not configured.")) return json_error(_("Big Blue Button is not configured."))
else: else:
try:
response = requests.get( response = requests.get(
add_query_to_redirect_url(settings.BIG_BLUE_BUTTON_URL + "api/create", urlencode({ add_query_to_redirect_url(settings.BIG_BLUE_BUTTON_URL + "api/create", urlencode({
"meetingID": meeting_id, "meetingID": meeting_id,
@@ -197,9 +198,8 @@ def join_bigbluebutton(request: HttpRequest, meeting_id: str = REQ(validator=che
"attendeePW": password + "a", "attendeePW": password + "a",
"checksum": checksum "checksum": checksum
}))) })))
try:
response.raise_for_status() response.raise_for_status()
except Exception: except requests.RequestException:
return json_error(_("Error connecting to the Big Blue Button server.")) return json_error(_("Error connecting to the Big Blue Button server."))
payload = ElementTree.fromstring(response.text) payload = ElementTree.fromstring(response.text)