Skip to content

Commit

Permalink
fix: add better request error handling during annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Dec 14, 2022
1 parent 901ed97 commit e60a719
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions robotoff/insights/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from enum import Enum
from typing import Optional

from requests.exceptions import HTTPError, JSONDecodeError, SSLError, Timeout

from robotoff.insights.dataclass import InsightType
from robotoff.insights.normalize import normalize_emb_code
from robotoff.models import ProductInsight, db
Expand Down Expand Up @@ -54,6 +56,7 @@ class AnnotationStatus(Enum):
error_missing_data = 7
error_invalid_image = 8
vote_saved = 9
error_failed_update = 10


SAVED_ANNOTATION_RESULT = AnnotationResult(
Expand Down Expand Up @@ -91,6 +94,11 @@ class AnnotationStatus(Enum):
status=AnnotationStatus.vote_saved.name,
description="the annotation vote was saved",
)
FAILED_UPDATE_RESULT = AnnotationResult(
status_code=AnnotationStatus.error_failed_update.value,
status=AnnotationStatus.error_failed_update.name,
description="Open Food Facts update failed",
)


class InsightAnnotator(metaclass=abc.ABCMeta):
Expand All @@ -103,8 +111,23 @@ def annotate(
auth: Optional[OFFAuthentication] = None,
automatic: bool = False,
) -> AnnotationResult:
with db.atomic():
return self._annotate(insight, annotation, update, data, auth, automatic)
with db.atomic() as tx:
try:
return self._annotate(
insight, annotation, update, data, auth, automatic
)
except (
HTTPError,
JSONDecodeError,
Timeout,
SSLError,
) as e:
logger.info(
f"Error occurred during OFF update: {type(e).__name__}, {e}"
)
logger.info("Rolling back SQL transaction")
tx.rollback()
return FAILED_UPDATE_RESULT

def _annotate(
self,
Expand Down

0 comments on commit e60a719

Please sign in to comment.