diff --git a/tom_observations/facilities/ocs.py b/tom_observations/facilities/ocs.py index 94db8de0c..894a27608 100644 --- a/tom_observations/facilities/ocs.py +++ b/tom_observations/facilities/ocs.py @@ -1359,13 +1359,18 @@ def get_facility_status(self): """ # make the request to the OCS API for the telescope_states now = datetime.now() - response = make_request( - 'GET', - urljoin(self.facility_settings.get_setting('portal_url'), '/api/telescope_states/'), - headers=self._portal_headers() - ) - telescope_states = response.json() - logger.warning(f"Telescope states took {(datetime.now() - now).total_seconds()}") + telescope_states = {} + try: + response = make_request( + 'GET', + urljoin(self.facility_settings.get_setting('portal_url'), '/api/telescope_states/'), + headers=self._portal_headers() + ) + response.raise_for_status() + telescope_states = response.json() + logger.info(f"Telescope states took {(datetime.now() - now).total_seconds()}") + except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e: + logger.warning(f"Error retrieving telescope_states from OCS for facility status: {repr(e)}") # Now, transform the telescopes_state dictionary in a dictionary suitable # for the facility_status.html template partial. diff --git a/tom_observations/tests/facilities/test_ocs.py b/tom_observations/tests/facilities/test_ocs.py index 338d57332..7d95ac31f 100644 --- a/tom_observations/tests/facilities/test_ocs.py +++ b/tom_observations/tests/facilities/test_ocs.py @@ -224,6 +224,15 @@ class TestOCSFacility(TestCase): def setUp(self): self.lco = OCSFacility() + @patch('tom_observations.facilities.ocs.make_request') + def test_get_facility_status_fails_gracefully(self, mock_make_request): + mock_response = Response() + mock_response._content = str.encode('ConnectionError - Error retrieving telescope states') + mock_response.status_code = 502 + mock_make_request.return_value = mock_response + facility_status = self.lco.get_facility_status() + self.assertEqual(facility_status.get('sites'), []) + @patch('tom_observations.facilities.ocs.make_request') def test_get_requestgroup_id(self, mock_make_request): mock_response = Response()