Skip to content

Commit 6de372a

Browse files
author
rrajeevan
committed
Adding new exception:ArgusObjectNotFoundException
1 parent 653d74d commit 6de372a

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

argusclient/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
77
#
88

9-
from .client import ArgusServiceClient, ArgusException, ArgusAuthException, MetricQuery, AnnotationQuery
9+
from .client import ArgusServiceClient, ArgusException, ArgusAuthException, ArgusObjectNotFoundException, MetricQuery, AnnotationQuery
1010
from .model import Namespace, Metric, Annotation, Dashboard, Alert, Trigger, Notification, User, AddListResult

argusclient/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class ArgusAuthException(ArgusException):
3434
"""
3535
pass
3636

37+
class ArgusObjectNotFoundException(ArgusException):
38+
"""
39+
An exception type that is thrown for Argus object not found errors.
40+
"""
41+
pass
3742

3843
class BaseQuery(object):
3944
def __init__(self, baseExpr, *tailParams, **kwargs):
@@ -741,9 +746,9 @@ def check_success(resp, decCls):
741746
raise ArgusException(resp.text)
742747
return res
743748
elif resp.status_code == httplib.NOT_FOUND:
744-
raise ArgusException("Object not found at endpoint: %s message: %s" % (resp.request.url, resp.text))
749+
raise ArgusObjectNotFoundException("Object not found at endpoint: %s message: %s" % (resp.url, resp.text))
745750
elif resp.status_code == httplib.UNAUTHORIZED:
746-
raise ArgusAuthException("Failed to authenticate at endpoint: %s message: %s" % (resp.request.url, resp.text))
751+
raise ArgusAuthException("Failed to authenticate at endpoint: %s message: %s" % (resp.url, resp.text))
747752
else:
748753
# TODO handle this differently, as this is typically a more severe exception (see W-2830904)
749754
raise ArgusException(resp.text)

tests/test_service.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ def __init__(self, url):
1919

2020

2121
class MockResponse(object):
22-
def __init__(self, json_text, status_code, request=None):
22+
def __init__(self, json_text, status_code, request=None,url=None):
2323
self.text = json_text
2424
self.status_code = status_code
2525
self.cookies = cookies
2626
self.request = request
27+
self.url = url
2728

2829
def json(self, **kwargs):
2930
return json.loads(self.text, **kwargs)
@@ -47,8 +48,11 @@ def testFailure(self):
4748
def testError(self):
4849
self.failUnlessRaises(ArgusException, lambda: check_success(MockResponse("", 500), decCls=JsonDecoder))
4950

51+
def testUnauthorized(self):
52+
self.failUnlessRaises(ArgusAuthException, lambda: check_success(MockResponse("", 401), decCls=JsonDecoder))
53+
5054
def testUnexpectedEndpoint(self):
51-
self.failUnlessRaises(Exception, lambda: check_success(MockResponse("HTTP 404 Not Found", 404), decCls=JsonDecoder))
55+
self.failUnlessRaises(ArgusObjectNotFoundException, lambda: check_success(MockResponse("HTTP 404 Not Found", 404), decCls=JsonDecoder))
5256

5357

5458
class TestServiceBase(unittest.TestCase):
@@ -92,7 +96,7 @@ def testAuthImplicit(self):
9296

9397
@mock.patch('requests.Session.post', return_value=MockResponse("""{ "status": 401, "message": "Unauthorized" }""", 401, request=MockRequest("v2/auth/login")))
9498
def testUnauthorized(self, mockPost):
95-
"""A stright-forward login failure with invalid username/password"""
99+
"""A straight-forward login failure with invalid username/password"""
96100
self.failUnlessRaises(ArgusAuthException, lambda: self.argus.login())
97101

98102
def testAuthWithDirectRefreshToken(self):
@@ -423,18 +427,18 @@ def testGetUserAlert(self, mockGet):
423427
res = self.argus.alerts.get_user_alert(testId, testId)
424428
self.assertTrue(isinstance(res, Alert))
425429
self.assertEquals(res.to_dict(), alert_D)
426-
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
430+
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))
427431

428432
@mock.patch('requests.Session.get', return_value=MockResponse(json.dumps([]), 200))
429433
def testGetUserAlertNoMatch(self, mockGet):
430434
res = self.argus.alerts.get_user_alert(testId, testId)
431435
self.assertEquals(res, None)
432-
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
436+
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))
433437

434438
@mock.patch('requests.Session.get', return_value=MockResponse(json.dumps([alert_D, alert_D]), 200))
435439
def testGetUserAlertUnexpectedMultiple(self, mockGet):
436440
self.failUnlessRaises(AssertionError, lambda: self.argus.alerts.get_user_alert(testId, testId))
437-
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
441+
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))
438442

439443

440444
class TestAlertTrigger(TestServiceBase):

0 commit comments

Comments
 (0)