Reviewers: ,


Please review this at http://codereview.tryton.org/94001/

Affected files:
  M trytond/error.py
  A trytond/exceptions.py
  M trytond/model/modelsql.py
  M trytond/protocols/dispatcher.py
  M trytond/protocols/jsonrpc.py
  M trytond/protocols/webdav.py
  M trytond/protocols/xmlrpc.py
  M trytond/security.py


Index: trytond/error.py
===================================================================
--- a/trytond/error.py
+++ b/trytond/error.py
@@ -2,6 +2,7 @@
 #this repository contains the full copyright notices and license terms.
 from trytond.transaction import Transaction
 from trytond.pool import Pool
+from trytond.exceptions import UserError, UserWarning


 class WarningErrorMixin(object):
@@ -68,11 +69,11 @@
                 except TypeError:
                     pass
             if raise_exception:
-                raise Exception('UserError', error, error_description)
+                raise UserError(error, error_description)
             else:
                 return (error, error_description)
         if raise_exception:
-            raise Exception('UserError', error)
+            raise UserError(error)
         else:
             return error

@@ -101,9 +102,8 @@
                         error_description=warning_description,
                         error_description_args=warning_description_args,
                         raise_exception=False)
-                raise Exception('UserWarning', warning_name, warning,
-                        warning_description)
+ raise UserWarning(warning_name, warning, warning_description)
             else:
                 warning = self.raise_user_error(warning,
                         error_args=warning_args, raise_exception=False)
-                raise Exception('UserWarning', warning_name, warning)
+                raise UserWarning(warning_name, warning)
Index: trytond/exceptions.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/trytond/exceptions.py
@@ -0,0 +1,37 @@
+#This file is part of Tryton.  The COPYRIGHT file at the top level of
+#this repository contains the full copyright notices and license terms.
+
+class UserError(Exception):
+
+    def __init__(self, message, description=''):
+ super(UserError, self).__init__('UserError', (message, description))
+        self.message = message
+        self.description = description
+        self.code = 1
+
+
+class UserWarning(Exception):
+
+    def __init__(self, name, message, description=''):
+        super(UserWarning, self).__init__('UserWarning', (name, message,
+                description))
+        self.name = name
+        self.message = message
+        self.description = description
+        self.code = 2
+
+
+class NotLogged(Exception):
+
+    def __init__(self):
+        super(NotLogged, self).__init__('NotLogged')
+        self.code = 3
+
+
+class ConcurrencyException(Exception):
+
+    def __init__(self, message):
+        super(ConcurrencyException, self).__init__('ConcurrencyException',
+            message)
+        self.message = message
+        self.code = 4
Index: trytond/model/modelsql.py
===================================================================
--- a/trytond/model/modelsql.py
+++ b/trytond/model/modelsql.py
@@ -15,6 +15,7 @@
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.cache import LRUDict
+from trytond.exceptions import ConcurrencyException
 _RE_UNIQUE = re.compile('UNIQUE\s*\((.*)\)', re.I)
 _RE_CHECK = re.compile('CHECK\s*\((.*)\)', re.I)

@@ -777,8 +778,8 @@
                             'WHERE ' + ' OR '.join(
                                 (clause,) * (len(args) // 2)), args)
                     if cursor.fetchone():
-                        raise Exception('ConcurrencyException',
-                                'Records were modified in the meanwhile')
+                        raise ConcurrencyException(
+                            'Records were modified in the meanwhile')
             for i in ids:
                 if Transaction().timestamp.get(self._name + ',' + str(i)):
                     del Transaction().timestamp[self._name + ',' +str(i)]
@@ -1005,8 +1006,8 @@
                             'WHERE ' + ' OR '.join(
                                 (clause,) * (len(args)/2)), args)
                     if cursor.fetchone():
-                        raise Exception('ConcurrencyException',
-                                'Records were modified in the meanwhile')
+                        raise ConcurrencyException(
+                            'Records were modified in the meanwhile')
             for i in ids:
                 if Transaction().timestamp.get(self._name + ',' + str(i)):
                     del Transaction().timestamp[self._name + ',' +str(i)]
Index: trytond/protocols/dispatcher.py
===================================================================
--- a/trytond/protocols/dispatcher.py
+++ b/trytond/protocols/dispatcher.py
@@ -20,6 +20,8 @@
 from trytond.monitor import monitor
 from trytond.transaction import Transaction
 from trytond.cache import Cache
+from trytond.exceptions import UserError, UserWarning, NotLogged, \
+    ConcurrencyException

def dispatch(host, port, protocol, database_name, user, session, object_type,
         object_name, method, *args, **kargs):
@@ -132,9 +134,8 @@
     obj = pool.get(object_name, type=object_type)

     if method not in obj._rpc:
-        raise Exception('UserError', 'Calling method %s on ' \
-                '%s %s is not allowed!' % \
-                (method, object_type, object_name))
+        raise UserError('Calling method %s on %s %s is not allowed!'
+            % (method, object_type, object_name))

     readonly = not obj._rpc[method]

@@ -155,10 +156,9 @@
             if not readonly:
                 transaction.cursor.commit()
         except Exception, exception:
-            if CONFIG['verbose'] or (exception.args \
-                    and str(exception.args[0]) not in \
-                    ('NotLogged', 'ConcurrencyException', 'UserError',
-                        'UserWarning')):
+            if CONFIG['verbose'] and not isinstance(exception, (
+                        NotLogged, ConcurrencyException, UserError,
+                        UserWarning)):
                 tb_s = reduce(lambda x, y: x + y,
                         traceback.format_exception(*sys.exc_info()))
                 logger = logging.getLogger('dispatcher')
Index: trytond/protocols/jsonrpc.py
===================================================================
--- a/trytond/protocols/jsonrpc.py
+++ b/trytond/protocols/jsonrpc.py
@@ -5,6 +5,8 @@
 from trytond.config import CONFIG
 from trytond.protocols.datatype import Float
 from trytond.protocols.common import daemon, GZipRequestHandlerMixin
+from trytond.exceptions import UserError, UserWarning, NotLogged, \
+    ConcurrencyException
 import SimpleXMLRPCServer
 import SimpleHTTPServer
 import SocketServer
@@ -91,6 +93,9 @@
                 response['result'] = dispatch_method(method, params)
             else:
                 response['result'] = self._dispatch(method, params)
+        except (UserError, UserWarning, NotLogged,
+                ConcurrencyException), exception:
+            response['error'] = exception.args
         except Exception:
             tb_s = ''
             for line in traceback.format_exception(*sys.exc_info()):
Index: trytond/protocols/webdav.py
===================================================================
--- a/trytond/protocols/webdav.py
+++ b/trytond/protocols/webdav.py
@@ -27,6 +27,8 @@
 from trytond.pool import Pool
 from trytond.transaction import Transaction
 from trytond.cache import Cache
+from trytond.exceptions import UserError, UserWarning, NotLogged, \
+    ConcurrencyException
 _TRYTON_RELOAD = False
 domimpl = xml.dom.minidom.getDOMImplementation()

@@ -133,12 +135,9 @@
         self.verbose = False

     def _log_exception(self, exception):
-        if CONFIG['verbose'] or (exception.args \
-                and (str(exception.args[0]) not in \
-                ('NotLogged', 'ConcurrencyException', 'UserError',
-                    'UserWarning')) \
-                    and not isinstance(exception,
- (DAV_Error, DAV_NotFound, DAV_Secret, DAV_Forbidden))):
+        if CONFIG['verbose'] and not isinstance(exception, (
+ NotLogged, ConcurrencyException, UserError, UserWarning,
+                    DAV_Error, DAV_NotFound, DAV_Secret, DAV_Forbidden)):
             tb_s = reduce(lambda x, y: x + y,
                 traceback.format_exception(*sys.exc_info()))
             logger = logging.getLogger('webdav')
Index: trytond/protocols/xmlrpc.py
===================================================================
--- a/trytond/protocols/xmlrpc.py
+++ b/trytond/protocols/xmlrpc.py
@@ -81,6 +81,10 @@
                     }
                 return dispatch(host, port, 'XML-RPC', database_name, user,
                         session, object_type, object_name, method, *params)
+            except (UserError, UserWarning, NotLogged,
+                    ConcurrencyException), exception:
+                        raise xmlrpclib.Fault(exception.code,
+                            '\n'.join(exception.args))
             except Exception:
                 tb_s = ''
                 for line in traceback.format_exception(*sys.exc_info()):
@@ -95,7 +99,7 @@
                     import pdb
                     traceb = sys.exc_info()[2]
                     pdb.post_mortem(traceb)
-                raise xmlrpclib.Fault(1, str(sys.exc_value) + '\n' + tb_s)
+ raise xmlrpclib.Fault(255, str(sys.exc_value) + '\n' + tb_s)
         finally:
             security.logout(database_name, user, session)

Index: trytond/security.py
===================================================================
--- a/trytond/security.py
+++ b/trytond/security.py
@@ -5,6 +5,7 @@
 from trytond.pool import Pool
 from trytond.config import CONFIG
 from trytond.transaction import Transaction
+from trytond.exceptions import NotLogged
 import time


@@ -74,7 +75,7 @@
             del _USER_CACHE[dbname][user][i]
     if result:
         return result
-    raise Exception('NotLogged')
+    raise NotLogged()

 def get_connections(dbname, user):
     res = 0


--
[email protected] mailing list

Reply via email to