From ca74aab202b13926a92f9287746a5ee7551e3281 Mon Sep 17 00:00:00 2001 From: ahmedk <karim.ahmed@xfel.eu> Date: Tue, 2 Apr 2024 12:37:54 +0200 Subject: [PATCH] catch CALCATAPIError and create exception classes for two cases in webservice --- webservice/messages.py | 10 +++++++++ webservice/webservice.py | 45 +++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/webservice/messages.py b/webservice/messages.py index 48b17b367..2c586a3a8 100644 --- a/webservice/messages.py +++ b/webservice/messages.py @@ -33,6 +33,16 @@ class MDC: NOTHING_TO_DO = "Nothing to calibrate for this run, copied raw data only" +class PDUsNotFoundError(Exception): + """Exception raised when a detector cannot be found.""" + pass + + +class OperationModeNotFoundError(Exception): + """Exception raised when an operation mode cannot be found for a given detector.""" + pass + + class Success: UPLOADED_CONFIG = "SUCCESS: Uploaded config for cycle {}, proposal {}" START_CORRECTION = "SUCCESS: Started correction: proposal {}, run {}" diff --git a/webservice/webservice.py b/webservice/webservice.py index 5f0f1bd1f..9cd06b81c 100644 --- a/webservice/webservice.py +++ b/webservice/webservice.py @@ -31,10 +31,17 @@ from metadata_client.metadata_client import MetadataClient try: from .config import webservice as config - from .messages import MDC, Errors, MigrationError, Success + from .messages import ( + MDC, + Errors, + MigrationError, + OperationModeNotFoundError, + PDUsNotFoundError, + Success, + ) except ImportError: from config import webservice as config - from messages import MDC, Errors, MigrationError, Success + from messages import MDC, Errors, MigrationError, Success, PDUsNotFoundError, OperationModeNotFoundError from cal_tools.calcat_interface2 import get_client @@ -347,23 +354,37 @@ def get_number_of_characterization_runs(karabo_id, operation_mode): """ client = get_client() detector_id = client.detector_by_identifier(karabo_id)['id'] - - pdus = client.get( - 'physical_detector_units/get_all_by_detector?' - f'detector_id={detector_id}') - first_pdu_id = pdus[0]['detector_type']['id'] - operation_modes = client.get( - 'operation_modes/get_all_by_detector_type?' - f'detector_type_id={first_pdu_id}') + try: + pdus = client.get( + 'physical_detector_units/get_all_by_detector?' + f'detector_id={detector_id}') + if len(pdus) == 0: + raise PDUsNotFoundError + except Exception as e: + raise PDUsNotFoundError( + f'Failed to find physical detector units for {karabo_id}: {e}') + + det_type = pdus[0]['detector_type'] + try: + operation_modes = client.get( + 'operation_modes/get_all_by_detector_type?' + f'detector_type_id={det_type["id"]}') + if len(operation_modes) == 0: + raise OperationModeNotFoundError + except Exception as e: + raise OperationModeNotFoundError( + f'Failed to find operation modes for {det_type["name"]}: {e}') number_of_runs = next( ( mode['number_of_runs'] for mode in operation_modes if mode['identifier'] == operation_mode), None) + if number_of_runs is None: - raise ValueError( - f"{karabo_id} doesn't have this operation mode {operation_mode}.") + raise OperationModeNotFoundError( + f'{karabo_id} does not have this operation mode {operation_mode}.') + return number_of_runs -- GitLab