diff --git a/chronos/__init__.py b/chronos/__init__.py index 78be996..b8099cc 100755 --- a/chronos/__init__.py +++ b/chronos/__init__.py @@ -32,6 +32,10 @@ class ChronosAPIError(Exception): pass +class UnauthorizedError(Exception): + pass + + class MissingFieldError(Exception): pass @@ -132,7 +136,7 @@ def _call(self, url, method="GET", body=None, headers={}): response = self._check(*conn.request(endpoint, method, body=body, headers=hdrs)) self.logger.info('Got response from %s', endpoint) return response - except Exception as e: + except ChronosAPIError as e: self.logger.error('Error while calling %s: %s', endpoint, e.message) raise ChronosAPIError('No remaining Chronos servers to try') @@ -141,6 +145,10 @@ def _check(self, resp, content): status = resp.status self.logger.debug("status: %d" % status) payload = None + + if status == 401: + raise UnauthorizedError() + if content: try: payload = json.loads(content) diff --git a/tests/test_chronos_init.py b/tests/test_chronos_init.py index 781e243..f00d612 100644 --- a/tests/test_chronos_init.py +++ b/tests/test_chronos_init.py @@ -33,8 +33,8 @@ def test_check_accepts_json(): def test_check_returns_raw_response_when_not_json(): client = chronos.ChronosClient("localhost") fake_response = mock.Mock() - fake_response.status = 401 - fake_content = 'UNAUTHORIZED' + fake_response.status = 400 + fake_content = 'foo bar baz' actual = client._check(fake_response, fake_content) assert actual == fake_content @@ -82,6 +82,14 @@ def test_check_one_of_all(): client._check_fields(job) +def test_check_unauthorized_raises(): + client = chronos.ChronosClient(servers="localhost") + mock_response = mock.Mock() + mock_response.status = 401 + with pytest.raises(chronos.UnauthorizedError): + client._check(mock_response, '{"foo": "bar"}') + + @mock.patch('chronos.ChronosJob') def test_check_one_of_ok(patch_chronos_job): patch_chronos_job.one_of = ['foo', 'bar']