From 2eca302234823fa7d884484358a6bffe5cd45ded Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 12 Sep 2023 09:16:15 +0200 Subject: [PATCH] Refactor PostAT.get_status() to handle exceptions when fetching shipment status. Previously, an API call was made to get the shipment status using the tracking number. The code now incorporates exception handling to log any errors that occur during the fetching of the status. This change ensures that any potential errors are captured and logged for debugging purposes. --- src/trackbert/providers/postat.py | 32 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/trackbert/providers/postat.py b/src/trackbert/providers/postat.py index 7507f99..9416b12 100644 --- a/src/trackbert/providers/postat.py +++ b/src/trackbert/providers/postat.py @@ -2,6 +2,7 @@ from ..classes.provider import BaseProvider from ..classes.database import Event import json +import logging from dateutil.parser import parse from postat.classes.api import PostAPI @@ -13,20 +14,25 @@ class PostAT(BaseProvider): def get_status(self, tracking_number, carrier): api = PostAPI() - status = api.get_shipment_status(tracking_number) - shipment = status["data"]["einzelsendung"] - events = shipment["sendungsEvents"] - for event in events: - timestamp = event["timestamp"] - py_timestamp = parse(timestamp) - event_time = py_timestamp.strftime("%Y-%m-%d %H:%M:%S") - yield Event( - shipment_id=0, - event_time=event_time, - event_description=event["text"], - raw_event=json.dumps(event), - ) + try: + status = api.get_shipment_status(tracking_number) + shipment = status["data"]["einzelsendung"] + events = shipment["sendungsEvents"] + + for event in events: + timestamp = event["timestamp"] + py_timestamp = parse(timestamp) + event_time = py_timestamp.strftime("%Y-%m-%d %H:%M:%S") + yield Event( + shipment_id=0, + event_time=event_time, + event_description=event["text"], + raw_event=json.dumps(event), + ) + + except Exception as e: + logging.error(f"Error while fetching status: {e}") def supported_carriers(self): return [