Skip to content

Commit 8fc924c

Browse files
committed
ruff: Fix B904 raise exceptions with raise ... from err.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 158480f commit 8fc924c

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

zulip/integrations/bridge_with_matrix/matrix_bridge.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def _matrix_to_zulip(self, room: nio.MatrixRoom, event: nio.Event) -> None
117117
)
118118
except Exception as exception:
119119
# Generally raised when user is forbidden
120-
raise BridgeFatalZulipError(exception)
120+
raise BridgeFatalZulipError(exception) from exception
121121
if result["result"] != "success":
122122
# Generally raised when API key is invalid
123123
raise BridgeFatalZulipError(result["msg"])
@@ -459,7 +459,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]:
459459
try:
460460
config.read(config_file)
461461
except configparser.Error as exception:
462-
raise BridgeConfigError(str(exception))
462+
raise BridgeConfigError(str(exception)) from exception
463463

464464
if set(config.sections()) < {"matrix", "zulip"}:
465465
raise BridgeConfigError("Please ensure the configuration has zulip & matrix sections.")
@@ -619,14 +619,16 @@ def write_sample_config(target_path: str, zuliprc: Optional[str]) -> None:
619619
try:
620620
zuliprc_config.read(zuliprc)
621621
except configparser.Error as exception:
622-
raise BridgeConfigError(str(exception))
622+
raise BridgeConfigError(str(exception)) from exception
623623

624624
try:
625625
sample_dict["zulip"]["email"] = zuliprc_config["api"]["email"]
626626
sample_dict["zulip"]["site"] = zuliprc_config["api"]["site"]
627627
sample_dict["zulip"]["api_key"] = zuliprc_config["api"]["key"]
628628
except KeyError as exception:
629-
raise BridgeConfigError("You provided an invalid zuliprc file: " + str(exception))
629+
raise BridgeConfigError(
630+
"You provided an invalid zuliprc file: " + str(exception)
631+
) from exception
630632

631633
sample: configparser.ConfigParser = configparser.ConfigParser()
632634
sample.read_dict(sample_dict)

zulip/zulip/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,23 @@ def end_error_retry(succeeded: bool) -> None:
650650
isinstance(e, requests.exceptions.SSLError)
651651
and str(e) != "The read operation timed out"
652652
):
653-
raise UnrecoverableNetworkError("SSL Error")
653+
raise UnrecoverableNetworkError("SSL Error") from e
654654
if longpolling:
655655
# When longpolling, we expect the timeout to fire,
656656
# and the correct response is to just retry
657657
continue
658658
else:
659659
end_error_retry(False)
660660
raise
661-
except requests.exceptions.ConnectionError:
661+
except requests.exceptions.ConnectionError as e:
662662
if not self.has_connected:
663663
# If we have never successfully connected to the server, don't
664664
# go into retry logic, because the most likely scenario here is
665665
# that somebody just hasn't started their server, or they passed
666666
# in an invalid site.
667-
raise UnrecoverableNetworkError("cannot connect to server " + self.base_url)
667+
raise UnrecoverableNetworkError(
668+
"cannot connect to server " + self.base_url
669+
) from e
668670

669671
if error_retry(""):
670672
continue

zulip_bots/zulip_bots/bots/giphy/giphy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ def validate_config(config_info: Dict[str, str]) -> None:
3434
data = requests.get(GIPHY_TRANSLATE_API, params=query)
3535
data.raise_for_status()
3636
except ConnectionError as e:
37-
raise ConfigValidationError(str(e))
37+
raise ConfigValidationError(str(e)) from e
3838
except HTTPError as e:
3939
error_message = str(e)
4040
if data.status_code == 403:
4141
error_message += (
4242
"This is likely due to an invalid key.\n"
4343
"Follow the instructions in doc.md for setting an API key."
4444
)
45-
raise ConfigValidationError(error_message)
45+
raise ConfigValidationError(error_message) from e
4646

4747
def initialize(self, bot_handler: BotHandler) -> None:
4848
self.config_info = bot_handler.get_config_info("giphy")
@@ -76,8 +76,8 @@ def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]:
7676

7777
try:
7878
gif_url = data.json()["data"]["images"]["original"]["url"]
79-
except (TypeError, KeyError): # Usually triggered by no result in Giphy.
80-
raise GiphyNoResultError
79+
except (TypeError, KeyError) as e: # Usually triggered by no result in Giphy.
80+
raise GiphyNoResultError from e
8181
return gif_url
8282

8383

zulip_bots/zulip_bots/bots/mention/mention.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ def generate_response(self, keyword: str) -> str:
114114

115115
try:
116116
alert_id = self.get_alert_id(keyword)
117-
except (TypeError, KeyError):
117+
except (TypeError, KeyError) as e:
118118
# Usually triggered by invalid token or json parse error when account quote is finished.
119-
raise MentionNoResponseError
119+
raise MentionNoResponseError from e
120120

121121
try:
122122
mentions = self.get_mentions(alert_id)
123-
except (TypeError, KeyError):
123+
except (TypeError, KeyError) as e:
124124
# Usually triggered by no response or json parse error when account quota is finished.
125-
raise MentionNoResponseError
125+
raise MentionNoResponseError from e
126126

127127
reply = "The most recent mentions of `" + keyword + "` on the web are: \n"
128128
for mention in mentions:

zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def get_trivia_payload() -> Dict[str, Any]:
9797
try:
9898
data = requests.get(url)
9999

100-
except requests.exceptions.RequestException:
101-
raise NotAvailableError
100+
except requests.exceptions.RequestException as e:
101+
raise NotAvailableError from e
102102

103103
if data.status_code != 200:
104104
raise NotAvailableError

0 commit comments

Comments
 (0)