Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

python: Catch specific exceptions from orjson.

Anders Kaseorgaabef3d9be8c
diff --git a/tools/create-test-api-docs b/tools/create-test-api-docs
index e9aa9184e8c..672d424f440 100755
--- a/tools/create-test-api-docs
+++ b/tools/create-test-api-docs
@@ -41,7 +41,7 @@ def encode_info(info: Any) -> str:
try:
info = orjson.loads(info)
result = '(stringified)\n'
- except Exception:
+ except orjson.JSONDecodeError:
pass
result += html.escape(pprint.pformat(info, indent=4))
return '<pre>' + result + '</pre>'
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index db198192606..d46386faa0d 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -2039,7 +2039,7 @@ def extract_stream_indicator(s: str) -> Union[str, int]:
try:
data = orjson.loads(s)
- except (ValueError, TypeError):
+ except orjson.JSONDecodeError:
# If there was no JSON encoding, then we just
# have a raw stream name.
return s
@@ -2067,7 +2067,7 @@ def extract_private_recipients(s: str) -> Union[List[str], List[int]]:
try:
data = orjson.loads(s)
- except (ValueError, TypeError):
+ except orjson.JSONDecodeError:
data = s
if isinstance(data, str):
@@ -2388,7 +2388,7 @@ def check_message(sender: UserProfile, client: Client, addressee: Addressee,
if widget_content is not None:
try:
widget_content = orjson.loads(widget_content)
- except Exception:
+ except orjson.JSONDecodeError:
raise JsonableError(_('Widgets: API programmer sent invalid JSON content'))
try:
diff --git a/zerver/lib/request.py b/zerver/lib/request.py
index 099a1b390fe..3408d0e95f0 100644
--- a/zerver/lib/request.py
+++ b/zerver/lib/request.py
@@ -347,7 +347,7 @@ def has_request_variables(view_func: ViewFuncT) -> ViewFuncT:
if param.validator is not None and not default_assigned:
try:
val = orjson.loads(val)
- except Exception:
+ except orjson.JSONDecodeError:
raise JsonableError(_('Argument "{}" is not valid JSON.').format(post_var_name))
try:
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 0bbc3a0bf3c..731cbc296bf 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -745,7 +745,7 @@ Output:
"""
try:
json = orjson.loads(result.content)
- except Exception: # nocoverage
+ except orjson.JSONDecodeError: # nocoverage
json = {'msg': "Error parsing JSON in response!"}
self.assertEqual(result.status_code, 200, json['msg'])
self.assertEqual(json.get("result"), "success")
@@ -758,7 +758,7 @@ Output:
def get_json_error(self, result: HttpResponse, status_code: int=400) -> Dict[str, Any]:
try:
json = orjson.loads(result.content)
- except Exception: # nocoverage
+ except orjson.JSONDecodeError: # nocoverage
json = {'msg': "Error parsing JSON in response!"}
self.assertEqual(result.status_code, status_code, msg=json.get('msg'))
self.assertEqual(json.get("result"), "error")
diff --git a/zerver/views/submessage.py b/zerver/views/submessage.py
index 1f6ddf421d0..b032c8c888a 100644
--- a/zerver/views/submessage.py
+++ b/zerver/views/submessage.py
@@ -21,7 +21,7 @@ def process_submessage(request: HttpRequest,
try:
orjson.loads(content)
- except Exception:
+ except orjson.JSONDecodeError:
return json_error(_("Invalid json for submessage"))
do_add_submessage(
diff --git a/zproject/backends.py b/zproject/backends.py
index 14858e07cd7..9e3787a320b 100644
--- a/zproject/backends.py
+++ b/zproject/backends.py
@@ -14,6 +14,7 @@
# matching the args/kwargs passed in the authenticate() call.
import binascii
import copy
+import json
import logging
from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union, cast
@@ -1429,7 +1430,7 @@ class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
access_token = kwargs["response"]["access_token"]
try:
emails = self._user_data(access_token, '/emails')
- except (HTTPError, ValueError, TypeError): # nocoverage
+ except (HTTPError, json.JSONDecodeError): # nocoverage
# We don't really need an explicit test for this code
# path, since the outcome will be the same as any other
# case without any verified emails
@@ -1812,7 +1813,7 @@ class SAMLAuthBackend(SocialAuthMixin, SAMLAuth):
# IdP-initiated sign in. Right now we only support transporting subdomain through json in
# RelayState, but this format is nice in that it allows easy extensibility here.
return {'subdomain': data.get('subdomain')}
- except (ValueError, TypeError):
+ except orjson.JSONDecodeError:
return {}
def choose_subdomain(self, relayed_params: Dict[str, Any]) -> Optional[str]: