Commit 8a019a03 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

RAPI client: Include HTTP error code in GanetiApiError exception



Having the HTTP error code allows users of the class to act differently based
on the error code.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarDavid Knowles <dknowles@google.com>
parent de4f53e6
...@@ -74,7 +74,9 @@ class GanetiApiError(Error): ...@@ -74,7 +74,9 @@ class GanetiApiError(Error):
"""Generic error raised from Ganeti API. """Generic error raised from Ganeti API.
""" """
pass def __init__(self, msg, code=None):
Error.__init__(self, msg)
self.code = code
class InvalidReplacementMode(Error): class InvalidReplacementMode(Error):
...@@ -414,7 +416,7 @@ class GanetiRapiClient(object): ...@@ -414,7 +416,7 @@ class GanetiRapiClient(object):
else: else:
msg = str(response_content) msg = str(response_content)
raise GanetiApiError(msg) raise GanetiApiError(msg, code=resp.code)
return response_content return response_content
......
...@@ -151,6 +151,15 @@ class GanetiRapiClientTests(testutils.GanetiTestCase): ...@@ -151,6 +151,15 @@ class GanetiRapiClientTests(testutils.GanetiTestCase):
def assertDryRun(self): def assertDryRun(self):
self.assertTrue(self.rapi.GetLastHandler().dryRun()) self.assertTrue(self.rapi.GetLastHandler().dryRun())
def testHttpError(self):
self.rapi.AddResponse(None, code=404)
try:
self.client.GetJobStatus(15140)
except client.GanetiApiError, err:
self.assertEqual(err.code, 404)
else:
self.fail("Didn't raise exception")
def testGetVersion(self): def testGetVersion(self):
self.client._version = None self.client._version = None
self.rapi.AddResponse("2") self.rapi.AddResponse("2")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment