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 subprocess.

Anders Kaseorg17ac17286c0d
diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py
index 731cbc296bf..dd1f54827ed 100644
--- a/zerver/lib/test_classes.py
+++ b/zerver/lib/test_classes.py
@@ -2,6 +2,7 @@ import base64
import os
import re
import shutil
+import subprocess
import tempfile
import urllib
from contextlib import contextmanager
@@ -970,7 +971,7 @@ Output:
'''
with \
self.settings(ERROR_BOT=None), \
- mock.patch('zerver.lib.markdown.timeout', side_effect=KeyError('foo')), \
+ mock.patch('zerver.lib.markdown.timeout', side_effect=subprocess.CalledProcessError(1, [])), \
mock.patch('zerver.lib.markdown.markdown_logger'):
yield
diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py
index 6086ef9f86a..c8b4c5263a8 100644
--- a/zerver/logging_handlers.py
+++ b/zerver/logging_handlers.py
@@ -26,7 +26,7 @@ def try_git_describe() -> Optional[str]:
stderr=subprocess.PIPE,
cwd=os.path.join(os.path.dirname(__file__), '..'),
).strip().decode('utf-8')
- except Exception: # nocoverage
+ except (FileNotFoundError, subprocess.CalledProcessError): # nocoverage
return None
def add_request_metadata(report: Dict[str, Any], request: HttpRequest) -> None:
diff --git a/zerver/tests/test_report.py b/zerver/tests/test_report.py
index e8706f314b5..896d9f42288 100644
--- a/zerver/tests/test_report.py
+++ b/zerver/tests/test_report.py
@@ -1,3 +1,4 @@
+import subprocess
from typing import Any, Callable, Dict, Iterable, List, Tuple
from unittest import mock
@@ -148,7 +149,7 @@ class TestReport(ZulipTestCase):
subprocess_mock = mock.patch(
'zerver.views.report.subprocess.check_output',
- side_effect=KeyError('foo'),
+ side_effect=subprocess.CalledProcessError(1, []),
)
with mock_queue_publish('zerver.views.report.queue_json_publish') as m, subprocess_mock:
result = self.client_post("/json/report/error", params)
diff --git a/zerver/tests/test_zephyr.py b/zerver/tests/test_zephyr.py
index 8c6dffaf05c..8d70a520303 100644
--- a/zerver/tests/test_zephyr.py
+++ b/zerver/tests/test_zephyr.py
@@ -1,3 +1,4 @@
+import subprocess
from typing import Any
from unittest.mock import patch
@@ -51,7 +52,7 @@ class ZephyrTest(ZulipTestCase):
with \
ccache_mock(return_value=b'1234'), \
- ssh_mock(side_effect=KeyError('foo')), \
+ ssh_mock(side_effect=subprocess.CalledProcessError(1, [])), \
logging_mock() as log:
result = post("zephyr", cred=cred)
diff --git a/zerver/views/report.py b/zerver/views/report.py
index 05c2edca15b..5409e0d949a 100644
--- a/zerver/views/report.py
+++ b/zerver/views/report.py
@@ -108,7 +108,7 @@ def report_error(request: HttpRequest, user_profile: UserProfile, message: str=R
["git", "show", "-s", "--oneline"],
universal_newlines=True,
)
- except Exception:
+ except (FileNotFoundError, subprocess.CalledProcessError):
version = None
# Get the IP address of the request
diff --git a/zerver/views/zephyr.py b/zerver/views/zephyr.py
index 7047bb23a27..61d50cda365 100644
--- a/zerver/views/zephyr.py
+++ b/zerver/views/zephyr.py
@@ -66,7 +66,7 @@ def webathena_kerberos_login(request: HttpRequest, user_profile: UserProfile,
]
subprocess.check_call(["ssh", settings.PERSONAL_ZMIRROR_SERVER, "--",
" ".join(map(shlex.quote, command))])
- except Exception:
+ except subprocess.CalledProcessError:
logging.exception("Error updating the user's ccache", stack_info=True)
return json_error(_("We were unable to setup mirroring for you"))