From 02f2eead579c9707025a0fde10535515f90ea631 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Fri, 27 Feb 2026 16:23:53 +0100 Subject: [PATCH] Smargon": better handling of smargon connection exceptions --- src/aare/devices/smargon.py | 56 +++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/aare/devices/smargon.py b/src/aare/devices/smargon.py index adb99f4c..58b2c5a4 100644 --- a/src/aare/devices/smargon.py +++ b/src/aare/devices/smargon.py @@ -5,6 +5,7 @@ import requests from aare.common.beamline import MXBeamline from aare.common.coordinate import SmargonCoordinate, Coordinate, AerotechCoordinate +from aare.common.exception_handler import SmargonCommunicationError class SmargonMode(Enum): @@ -35,21 +36,60 @@ class Smargon(object): raise Exception("unknown beamline") def gonget(self, thing: str) -> dict: - """issue a GET for some API component on the smargopolo server""" + """issue a GET for some API component on the smargopolo server + short hand for goniometer get""" cmd = f"{self.__base}/{thing}" - r = requests.get(cmd) + try: + r = requests.get(cmd, timeout=2.0) + except requests.exceptions.RequestException as e: + raise SmargonCommunicationError( + f"Smargon GET failed for '{thing}'", + endpoint=thing, + base_url=self.__base, + operation="GET", + ) from e + if not r.ok: - raise Exception( - f"error getting {thing}; server returned {r.status_code} => {r.reason}" + raise SmargonCommunicationError( + f"Smargon GET returned HTTP {r.status_code} for '{thing}': {r.reason}", + endpoint=thing, + base_url=self.__base, + operation="GET", + status_code=r.status_code, ) - return r.json() + + try: + return r.json() + except ValueError as e: + raise SmargonCommunicationError( + f"Smargon GET returned invalid JSON for '{thing}'", + endpoint=thing, + base_url=self.__base, + operation="GET", + status_code=r.status_code, + ) from e def gonput(self, thing: str): + """issue a PUT command for some API component on the smargopolo server + short hand for goniometer put""" cmd = f"{self.__base}/{thing}" - r = requests.put(cmd) + try: + r = requests.put(cmd, timeout=2.0) + except requests.exceptions.RequestException as e: + raise SmargonCommunicationError( + f"Smargon PUT failed for '{thing}'", + endpoint=thing, + base_url=self.__base, + operation="PUT", + ) from e + if not r.ok: - raise Exception( - f"error putting {thing}; server returned {r.status_code} => {r.reason}" + raise SmargonCommunicationError( + f"Smargon PUT returned HTTP {r.status_code} for '{thing}': {r.reason}", + endpoint=thing, + base_url=self.__base, + operation="PUT", + status_code=r.status_code, ) def move_home(self, wait=False) -> None: