Browse Source

[2641] Unify exception handling code

Also don't catch specific exception errnos. Just handle them all
equally.
Mukund Sivaraman 12 years ago
parent
commit
ee109e14bb
2 changed files with 25 additions and 26 deletions
  1. 4 14
      src/bin/bindctl/bindcmd.py
  2. 21 12
      src/bin/bindctl/tests/bindctl_test.py

+ 4 - 14
src/bin/bindctl/bindcmd.py

@@ -215,10 +215,6 @@ WARNING: Python readline module isn't available, so the command line editor
 
         return True
 
-    def __print_check_ssl_msg(self):
-        self._print("Please check the logs of b10-cmdctl, there may "
-                    "have been a problem accepting SSL connections.")
-
     def _try_login(self, username, password):
         '''
         Attempts to log into cmdctl by sending a POST with the given
@@ -236,16 +232,10 @@ WARNING: Python readline module isn't available, so the command line editor
             data = response.read().decode()
             # return here (will raise error after try block)
             return (response, data)
-        except ssl.SSLError as err:
-            self._print("SSL error while sending login information: ", err)
-            if err.errno == ssl.SSL_ERROR_EOF:
-                self.__print_check_ssl_msg()
-        except socket.error as err:
-            self._print("Socket error while sending login information: ", err)
-            # An SSL setup error can also bubble up as a plain CONNRESET...
-            # (on some systems it usually does)
-            if err.errno == errno.ECONNRESET:
-                self.__print_check_ssl_msg()
+        except (ssl.SSLError, socket.error) as err:
+            self._print("Error while sending login information: ", err)
+            self._print("Please check the logs of b10-cmdctl, there may "
+                        "have been a problem accepting SSL connections.")
             pass
         raise FailToLogin()
 

+ 21 - 12
src/bin/bindctl/tests/bindctl_test.py

@@ -387,7 +387,11 @@ class TestConfigCommands(unittest.TestCase):
             self.tool.send_POST = send_POST_raiseImmediately
             self.assertRaises(FailToLogin, self.tool._try_login, "foo", "bar")
             expected_printed_messages.append(
-                'Socket error while sending login information:  test error')
+                'Error while sending login information:  test error')
+            expected_printed_messages.append(
+                'Please check the logs of b10-cmdctl, there may have been a '
+                'problem accepting SSL connections.'
+            )
             self.__check_printed_messages(expected_printed_messages)
 
             def create_send_POST_raiseOnRead(exception):
@@ -406,7 +410,11 @@ class TestConfigCommands(unittest.TestCase):
                 create_send_POST_raiseOnRead(socket.error("read error"))
             self.assertRaises(FailToLogin, self.tool._try_login, "foo", "bar")
             expected_printed_messages.append(
-                'Socket error while sending login information:  read error')
+                'Error while sending login information:  read error')
+            expected_printed_messages.append(
+                'Please check the logs of b10-cmdctl, there may have been a '
+                'problem accepting SSL connections.'
+            )
             self.__check_printed_messages(expected_printed_messages)
 
             # connection reset
@@ -416,12 +424,10 @@ class TestConfigCommands(unittest.TestCase):
                 create_send_POST_raiseOnRead(exc)
             self.assertRaises(FailToLogin, self.tool._try_login, "foo", "bar")
             expected_printed_messages.append(
-                'Socket error while sending login information:  '
-                'connection reset')
+                'Error while sending login information:  connection reset')
             expected_printed_messages.append(
-                'Please check the logs of b10-cmdctl, there may be a '
-                'problem accepting SSL connections, such as a permission '
-                'problem on the server certificate file.'
+                'Please check the logs of b10-cmdctl, there may have been a '
+                'problem accepting SSL connections.'
             )
             self.__check_printed_messages(expected_printed_messages)
 
@@ -431,7 +437,11 @@ class TestConfigCommands(unittest.TestCase):
                 create_send_POST_raiseOnRead(exc)
             self.assertRaises(FailToLogin, self.tool._try_login, "foo", "bar")
             expected_printed_messages.append(
-                'SSL error while sending login information:  .*')
+                'Error while sending login information:  .*')
+            expected_printed_messages.append(
+                'Please check the logs of b10-cmdctl, there may have been a '
+                'problem accepting SSL connections.'
+            )
             self.__check_printed_messages(expected_printed_messages)
 
             # 'EOF' SSL error
@@ -441,11 +451,10 @@ class TestConfigCommands(unittest.TestCase):
                 create_send_POST_raiseOnRead(exc)
             self.assertRaises(FailToLogin, self.tool._try_login, "foo", "bar")
             expected_printed_messages.append(
-                'SSL error while sending login information: .*')
+                'Error while sending login information: .*')
             expected_printed_messages.append(
-                'Please check the logs of b10-cmdctl, there may be a '
-                'problem accepting SSL connections, such as a permission '
-                'problem on the server certificate file.'
+                'Please check the logs of b10-cmdctl, there may have been a '
+                'problem accepting SSL connections.'
             )
             self.__check_printed_messages(expected_printed_messages)