exception handling for xmprpc devel
authorSandro Knauß <knauss@netzguerilla.net>
Thu, 26 Jan 2012 01:18:47 +0100
branchdevel
changeset 110 601fc908d9f1
parent 109 935b5fcaf152
child 111 8b493ab9c74f
exception handling for xmprpc better exception msg fpr velidate Exceptions
iro/error.py
iro/validate.py
iro/view/xmlrpc.py
--- a/iro/error.py	Thu Jan 26 01:15:33 2012 +0100
+++ b/iro/error.py	Thu Jan 26 01:18:47 2012 +0100
@@ -1,5 +1,4 @@
 # -*- coding: utf-8 -*-
-
 class InterfaceException(Exception):
     def __init__(self, code=999, msg="Unbekannter Fehler."):
         self.code=code
@@ -14,11 +13,25 @@
 
 class UserNotFound(InterfaceException):
     def __init__(self):
-        InterfaceException.__init__(self, 901, "Der API-Key ist ungültig.")
+        InterfaceException.__init__(self, 901, u"Der API-Key ist ungültig.")
 
 class ExternalException(InterfaceException):
     def __init__(self):
         InterfaceException.__init__(self, 950, "Fehler in externer API.")
 
 class ValidateException(Exception):
-    pass
+    def __init__(self, code=700, field=None, msg=None):
+        self.code=code
+        self.field=field
+        self.msg='Validation failed.'
+        if field and not msg:
+            self.msg="Validation of '%s' failed."%field
+
+    def dict(self):
+        return {"code":self.code,
+                "msg":self.msg,
+                }
+    def __str__(self):
+        return "%i:%s"%(self.code,self.msg)
+
+
--- a/iro/validate.py	Thu Jan 26 01:15:33 2012 +0100
+++ b/iro/validate.py	Thu Jan 26 01:18:47 2012 +0100
@@ -2,10 +2,10 @@
 from inspect import getcallargs
 from .error import ValidateException
 
-def vuserhash(hash):
+def vuserhash(hash,field):
     '''vailidate function for userhash'''
     if not re.match(r'^[a-f0-9]{15,}$', hash.lower()):
-        raise ValidateException()
+        raise ValidateException(field=field)
     return True
 
 def validate(**kargs):
@@ -19,7 +19,7 @@
         def new_f(*a,**k):
             kp=getcallargs(f,*a,**k)
             for i in kargs:
-                kargs[i](kp[i])
+                kargs[i](kp[i],i)
             return f(*a,**k)
         new_f.__name__ = f.__name__
         return new_f
--- a/iro/view/xmlrpc.py	Thu Jan 26 01:15:33 2012 +0100
+++ b/iro/view/xmlrpc.py	Thu Jan 26 01:18:47 2012 +0100
@@ -18,6 +18,7 @@
 
 from ..controller.viewinterface import Interface
 
+from ..error import InterfaceException, ValidateException
 
 class TwistedInterface(Interface):
     
@@ -45,7 +46,6 @@
         TwistedInterface.__init__(self)
     
     def lookupProcedure(self, procedurePath):
-        logging.debug("lookupProcedure('%s')"%procedurePath)
         if procedurePath not in self.listProcedures():
             raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
                         "procedure %s not found" % procedurePath)
@@ -55,6 +55,14 @@
             raise xmlrpc.NoSuchFunction(self.NOT_FOUND,
                         "procedure %s not found" % procedurePath)
 
+    def _ebRender(self, failure):
+        if isinstance(failure.value, InterfaceException):
+            return xmlrpc.Fault(failure.value.code, failure.value.msg)
+        if isinstance(failure.value, ValidateException):
+            return xmlrpc.Fault(failure.value.code, failure.value.msg)
+        return xmlrpc.XMLRPC._ebRender(self, failure)
+
+
 class SOAPInterface(TwistedInterface,soap.SOAPPublisher): 
     def __init__(self):
         soap.SOAPPublisher.__init__(self)