From 75d167f8eed30bde1401d98a110b0d4811656542 Mon Sep 17 00:00:00 2001 From: Stuart McHattie Date: Mon, 11 Mar 2024 15:20:41 +0000 Subject: [PATCH] Raise TransientRabbitError if the Traction API requests fail --- tol_lab_share/messages/traction/qc_message.py | 9 ++++++++- tol_lab_share/messages/traction/reception_message.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tol_lab_share/messages/traction/qc_message.py b/tol_lab_share/messages/traction/qc_message.py index 07eee957..b5021f64 100644 --- a/tol_lab_share/messages/traction/qc_message.py +++ b/tol_lab_share/messages/traction/qc_message.py @@ -5,6 +5,7 @@ from requests import codes, post +from lab_share_lib.exceptions import TransientRabbitError from tol_lab_share import error_codes from tol_lab_share.constants import OUTPUT_TRACTION_MESSAGE_SOURCE from tol_lab_share.error_codes import ErrorCode @@ -202,7 +203,13 @@ def send(self, url: str) -> bool: """ headers = {"Content-type": "application/vnd.api+json", "Accept": "application/vnd.api+json"} - r = post(url, headers=headers, data=dumps(self.payload(), default=str), verify=self._validate_certificates) + try: + r = post(url, headers=headers, data=dumps(self.payload(), default=str), verify=self._validate_certificates) + except Exception as ex: + logger.critical(f"Error submitting {self.__class__.__name__} to the Traction API.") + logger.exception(ex) + + raise TransientRabbitError(f"There was an error POSTing the {self.__class__.__name__} to the Traction API.") self._sent = r.status_code == codes.created if not self._sent: diff --git a/tol_lab_share/messages/traction/reception_message.py b/tol_lab_share/messages/traction/reception_message.py index cfc7d4f3..bfaf9815 100644 --- a/tol_lab_share/messages/traction/reception_message.py +++ b/tol_lab_share/messages/traction/reception_message.py @@ -1,9 +1,11 @@ from functools import singledispatchmethod import itertools +import logging from typing import Callable, Any from json import dumps from datetime import datetime from requests import post, codes +from lab_share_lib.exceptions import TransientRabbitError from tol_lab_share.messages.properties import MessageProperty from tol_lab_share.messages.properties.simple import Value from tol_lab_share.constants import ( @@ -15,6 +17,8 @@ from tol_lab_share.helpers import get_config from tol_lab_share.messages.rabbit.published import CreateLabwareFeedbackMessage +logger = logging.getLogger(__name__) + class TractionReceptionMessageRequest: """Class that manages the information of a single Traction request instance as part of the Traction message.""" @@ -317,7 +321,13 @@ def send(self, url: str) -> bool: """ headers = {"Content-type": "application/vnd.api+json", "Accept": "application/vnd.api+json"} - r = post(url, headers=headers, data=dumps(self.payload()), verify=self._validate_certificates) + try: + r = post(url, headers=headers, data=dumps(self.payload()), verify=self._validate_certificates) + except Exception as ex: + logger.critical(f"Error submitting {self.__class__.__name__} to the Traction API.") + logger.exception(ex) + + raise TransientRabbitError(f"There was an error POSTing the {self.__class__.__name__} to the Traction API.") self._sent = r.status_code == codes.created if not self._sent: