Having the HTTP error code allows users of the class to act differently based on the error code. --- lib/rapi/client.py | 6 ++++-- test/ganeti.rapi.client_unittest.py | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/rapi/client.py b/lib/rapi/client.py index c672d8a..b0745aa 100644 --- a/lib/rapi/client.py +++ b/lib/rapi/client.py @@ -74,7 +74,9 @@ class GanetiApiError(Error): """Generic error raised from Ganeti API. """ - pass + def __init__(self, msg, code=None): + Error.__init__(self, msg) + self.code = code class InvalidReplacementMode(Error): @@ -414,7 +416,7 @@ class GanetiRapiClient(object): else: msg = str(response_content) - raise GanetiApiError(msg) + raise GanetiApiError(msg, code=resp.code) return response_content diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py index abe3d33..6089352 100755 --- a/test/ganeti.rapi.client_unittest.py +++ b/test/ganeti.rapi.client_unittest.py @@ -151,6 +151,15 @@ class GanetiRapiClientTests(testutils.GanetiTestCase): def assertDryRun(self): 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): self.client._version = None self.rapi.AddResponse("2") -- 1.7.0.4