[or-cvs] [pytorctl/master 3/5] Expose status code and message separately in ErrorReply.

mikeperry at torproject.org mikeperry at torproject.org
Fri Aug 20 20:46:12 UTC 2010


Author: Harry Bock <hbock at ele.uri.edu>
Date: Sun, 1 Aug 2010 21:29:54 -0400
Subject: Expose status code and message separately in ErrorReply.
Commit: 55071c4f19934db703a2beeea4f8968bae636dc1

Allow callers to handle a specific status code as an integer as well as
the associated message within an ErrorReply exception.  Should be API-compatible
as the associated keyword arguments are popped from the **kwarg dict before
being passed to the parent exception class.
---
 TorCtl.py |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/TorCtl.py b/TorCtl.py
index 65b5608..1b91c4f 100755
--- a/TorCtl.py
+++ b/TorCtl.py
@@ -93,7 +93,12 @@ class ProtocolError(TorCtlError):
 
 class ErrorReply(TorCtlError):
   "Raised when Tor controller returns an error"
-  pass
+  def __init__(self, *args, **kwargs):
+    if "status" in kwargs:
+      self.status = kwargs.pop("status")
+    if "message" in kwargs:
+      self.message = kwargs.pop("message")
+    TorCtlError.__init__(self, *args, **kwargs)
 
 class NetworkStatus:
   "Filled in during NS events"
@@ -716,10 +721,12 @@ class Connection:
     assert msg.endswith("\r\n")
 
     lines = self._sendImpl(self._doSend, msg)
+
     # print lines
     for tp, msg, _ in lines:
       if tp[0] in '45':
-        raise ErrorReply("%s %s"%(tp, msg))
+        code = int(tp[:3])
+        raise ErrorReply("%s %s"%(tp, msg), status = code, message = msg)
       if tp not in expectedTypes:
         raise ProtocolError("Unexpectd message type %r"%tp)
 
-- 
1.7.1




More information about the tor-commits mailing list