From bdeb37b5e29aec15a5ac2ab51e6b69bd883027ad Mon Sep 17 00:00:00 2001 From: Alexander Steppke Date: Tue, 13 Feb 2024 17:36:54 +0100 Subject: [PATCH] improved error handling in pulse to timestamp --- src/cristallina/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cristallina/utils.py b/src/cristallina/utils.py index cffc0c9..16232d1 100644 --- a/src/cristallina/utils.py +++ b/src/cristallina/utils.py @@ -7,6 +7,7 @@ from collections import defaultdict import requests import datetime +from json import JSONDecodeError import logging @@ -675,9 +676,10 @@ def pulseid_to_timestamp(pulse_id): r = requests.get(f"https://data-api.psi.ch/api/4/map/pulse/sf-databuffer/{pulse_id}") try: ns_timestamp = r.json() - except JSONDecodeError as e: - raise ValueError(f"Cannot convert pulse id {pulse_id} to timestamp.") + timestamp = datetime.datetime.fromtimestamp(ns_timestamp/1E9) + except (JSONDecodeError, TypeError) as e: + raise ValueError(f"Cannot convert pulse id {pulse_id} to timestamp. Cause: {e}") - return datetime.datetime.fromtimestamp(ns_timestamp/1E9) + return timestamp