Skip to content

Commit

Permalink
fix: improve request exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Dec 15, 2022
1 parent 41d37f6 commit 36b675e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
11 changes: 6 additions & 5 deletions robotoff/insights/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ def annotate(
)
except HTTPError as e:
if e.response.status_code >= 500:
logger.info("HTTPError occurred during OFF update: %s", e)
logger.info(
"HTTPError occurred during OFF update: %s",
e.response.status_code,
)
logger.info("Rolling back SQL transaction")
tx.rollback()
return FAILED_UPDATE_RESULT
raise e
except (Timeout, SSLError) as e:
logger.info(
"Error occurred during OFF update: %s, %s", type(e).__name__, e
)
except (ConnectionError, Timeout, SSLError) as e:
logger.info("Error occurred during OFF update", exc_info=e)
logger.info("Rolling back SQL transaction")
tx.rollback()
return FAILED_UPDATE_RESULT
Expand Down
8 changes: 6 additions & 2 deletions robotoff/logos.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import operator
from typing import Optional

Expand Down Expand Up @@ -101,8 +102,11 @@ def get_stored_logo_ids() -> set[int]:
)

if not r.ok:
logger.warning(
f"error while fetching stored logo IDs ({r.status_code}): %s", r.text
logger.log(
logging.INFO if r.status_code >= 500 else logging.WARNING,
"error while fetching stored logo IDs (%s): %s",
r.status_code,
r.text,
)
return set()

Expand Down
12 changes: 7 additions & 5 deletions robotoff/metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import logging
from typing import Optional
from urllib.parse import urlparse

Expand Down Expand Up @@ -155,14 +156,15 @@ def generate_metrics_from_path(
try:
r = http_session.get(url, timeout=60, auth=settings._off_request_auth)
except (ConnectionError, SSLError, Timeout) as e:
logger.info(
"Error during metrics retrieval: url=%s, %s, %s", url, type(e).__name__, e
)
logger.info("Error during metrics retrieval: url=%s", url, exc_info=e)
return inserts

if not r.ok:
logger.info(
"HTTPError during metrics retrieval: url=%s, %s", url, r.status_code
logger.log(
logging.INFO if r.status_code >= 500 else logging.WARNING,
"HTTPError during metrics retrieval: url=%s, %s",
url,
r.status_code,
)
return inserts

Expand Down
17 changes: 11 additions & 6 deletions robotoff/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,22 @@ def get_image_from_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}"
error_message = "Cannot download image %s"
if error_raise:
raise ImageLoadingException(error_message) from e
logger.info(error_message)
raise ImageLoadingException(error_message % image_url) from e
logger.info(error_message, image_url, exc_info=e)
return None

if not r.ok:
error_message = f"Cannot download image {image_url}: HTTP {r.status_code}"
error_message = "Cannot download image %s: HTTP %s"
error_args = (image_url, r.status_code)
if error_raise:
raise ImageLoadingException(error_message)
logger.info(error_message)
raise ImageLoadingException(error_message % error_args)
logger.log(
logging.INFO if r.status_code >= 500 else logging.WARNING,
error_message,
*error_args,
)
return None

try:
Expand Down
12 changes: 2 additions & 10 deletions robotoff/workers/tasks/import_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,13 @@ def process_created_logos(image_prediction_id: int, server_domain: str):
try:
add_logos_to_ann(image_instance, logos)
except (ConnectionError, HTTPError, Timeout) as e:
logger.info(
"Request error during logo addition to ANN: %s, %s",
type(e).__name__,
e,
)
logger.info("Request error during logo addition to ANN", exc_info=e)
return

try:
save_nearest_neighbors(logos)
except (ConnectionError, HTTPError, Timeout) as e:
logger.info(
"Request error during ANN batch query: %s, %s",
type(e).__name__,
e,
)
logger.info("Request error during ANN batch query", exc_info=e)
return

thresholds = get_logo_confidence_thresholds()
Expand Down

0 comments on commit 36b675e

Please sign in to comment.