realm_emoji: Stop swallowing all exceptions from upload_emoji_image.

Putting all of the logic in a `finally` block is equivalent to a bare
`except` block, which silently consumes all exceptions.

Move only the most-necessary parts into the except; this lets
`BadImageError` exceptions from `zerver/lib/upload.py` to escape,
allowing better the generic "Image file upload failed" to be replaced
with a more specific message.

It also allows unexpected exceptions, as the previous commit resolved,
to escape and 500.  This lets them be detected and resolved, rather
than give users a silently bad experience.
This commit is contained in:
Alex Vandiver
2022-02-16 23:01:27 +00:00
committed by Tim Abbott
parent 96a5fa9d78
commit a40b3e1118
4 changed files with 19 additions and 20 deletions

View File

@@ -2288,17 +2288,17 @@ class UploadSpaceTests(UploadSerializeMixin, ZulipTestCase):
class DecompressionBombTests(ZulipTestCase):
def setUp(self) -> None:
super().setUp()
self.test_urls = {
"/json/users/me/avatar": "Image size exceeds limit.",
"/json/realm/logo": "Image size exceeds limit.",
"/json/realm/icon": "Image size exceeds limit.",
"/json/realm/emoji/bomb_emoji": "Image file upload failed.",
}
self.test_urls = [
"/json/users/me/avatar",
"/json/realm/logo",
"/json/realm/icon",
"/json/realm/emoji/bomb_emoji",
]
def test_decompression_bomb(self) -> None:
self.login("iago")
with get_test_image_file("bomb.png") as fp:
for url, error_string in self.test_urls.items():
for url in self.test_urls:
fp.seek(0, 0)
if url == "/json/realm/logo":
result = self.client_post(
@@ -2306,4 +2306,4 @@ class DecompressionBombTests(ZulipTestCase):
)
else:
result = self.client_post(url, {"f1": fp})
self.assert_json_error(result, error_string)
self.assert_json_error(result, "Image size exceeds limit.")