From 8a019a0342882be7cd495a356ba20479fd0176b3 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 11 May 2010 14:45:56 +0200
Subject: [PATCH] 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: Michael Hanselmann <hansmi@google.com>
Reviewed-by: David Knowles <dknowles@google.com>
---
 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 c672d8aae..b0745aabf 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 abe3d3385..6089352ad 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")
-- 
GitLab