improved error handling in pulse to timestamp

This commit is contained in:
2024-02-13 17:36:54 +01:00
parent cc893dcb22
commit bdeb37b5e2

View File

@@ -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