Skip to content

Commit

Permalink
fix: improve get_image_from_url exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Dec 14, 2022
1 parent e60a719 commit c5d0b3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions robotoff/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import requests
from PIL import Image
from requests.adapters import HTTPAdapter
from requests.exceptions import SSLError, Timeout

from robotoff import settings

Expand Down Expand Up @@ -176,16 +177,23 @@ def get_image_from_url(
:raises ImageLoadingException: _description_
:return: the Pillow Image or None.
"""
if session:
r = session.get(image_url)
else:
r = requests.get(image_url)
try:
if session:
r = session.get(image_url)
else:
r = requests.get(image_url)
except (ConnectionError, SSLError, Timeout) as e:
error_message = f"Cannot download image {image_url}: {type(e).__name__}, {e}"
if error_raise:
raise ImageLoadingException(error_message) from e
logger.info(error_message)
return None

if not r.ok:
error_message = f"Cannot load image {image_url}: HTTP {r.status_code}"
error_message = f"Cannot download image {image_url}: HTTP {r.status_code}"
if error_raise:
raise ImageLoadingException(error_message)
logger.warning(error_message)
logger.info(error_message)
return None

try:
Expand All @@ -194,12 +202,12 @@ def get_image_from_url(
error_message = f"Cannot identify image {image_url}"
if error_raise:
raise ImageLoadingException(error_message)
logger.warning(error_message)
logger.info(error_message)
except PIL.Image.DecompressionBombError:
error_message = f"Decompression bomb error for image {image_url}"
if error_raise:
raise ImageLoadingException(error_message)
logger.warning(error_message)
logger.info(error_message)

return None

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/utils/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_image_from_url_http_error(mocker):
response_mock.ok = False
response_mock.status_code = 404
session_mock.get.return_value = response_mock
with pytest.raises(ImageLoadingException, match="Cannot load image.*"):
with pytest.raises(ImageLoadingException, match="Cannot download image.*"):
get_image_from_url("MOCK_URL", error_raise=True, session=session_mock)

image = get_image_from_url("MOCK_URL", error_raise=False, session=session_mock)
Expand Down

0 comments on commit c5d0b3e

Please sign in to comment.