From e8dc0edbaa173a7367dc2abcd528a797f5af857b Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Tue, 27 Sep 2011 10:19:02 +0200 Subject: [PATCH] [IMP] xmlrpc: use some commented constant symobls instead of raw numbers. bzr revid: vmt@openerp.com-20110927081902-4zu2mri9zx8j27tb --- openerp/service/web_services.py | 4 ++-- openerp/wsgi.py | 32 +++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/openerp/service/web_services.py b/openerp/service/web_services.py index 181c7a25b78..a3bdb48a953 100644 --- a/openerp/service/web_services.py +++ b/openerp/service/web_services.py @@ -623,9 +623,9 @@ class wizard(netsvc.ExportService): if self.wiz_uid[wiz_id] == uid: return self._execute(db, uid, wiz_id, datas, action, context) else: - raise Exception, 'AccessDenied' + raise openerp.exceptions.AccessDenied() else: - raise Exception, 'WizardNotFound' + raise openerp.exceptions.Warning('Wizard not found.') # # TODO: set a maximum report number per user to avoid DOS attacks diff --git a/openerp/wsgi.py b/openerp/wsgi.py index f1886a4cb37..984b049d902 100644 --- a/openerp/wsgi.py +++ b/openerp/wsgi.py @@ -42,22 +42,40 @@ import openerp.modules import openerp.tools.config as config import service.websrv_lib as websrv_lib +# XML-RPC fault codes. Some care must be taken when changing these: the +# constants are also defined client-side and must remain in sync. +# User code must use the exceptions defined in ``openerp.exceptions`` (not +# create directly ``xmlrpclib.Fault`` objects). +XML_RPC_FAULT_CODE_APPLICATION_ERROR = 2 +XML_RPC_FAULT_CODE_ACCESS_DENIED = 3 +XML_RPC_FAULT_CODE_WARNING = 4 +XML_RPC_FAULT_CODE_ACCESS_ERROR = 5 + def xmlrpc_return(start_response, service, method, params): - """ Helper to call a service's method with some params, using a - wsgi-supplied ``start_response`` callback.""" - # This mimics SimpleXMLRPCDispatcher._marshaled_dispatch() for exception - # handling. + """ + Helper to call a service's method with some params, using a wsgi-supplied + ``start_response`` callback. + + This is the place to look at to see the mapping between core exceptions + and XML-RPC fault codes. + """ + # Map OpenERP core exceptions to XML-RPC fault codes. Specific exceptions + # defined in ``openerp.exceptions`` are mapped to specific fault codes; + # all the other exceptions are mapped to the generic + # XML_RPC_FAULT_CODE_APPLICATION_ERROR value. + # This also mimics SimpleXMLRPCDispatcher._marshaled_dispatch() for + # exception handling. try: result = openerp.netsvc.dispatch_rpc(service, method, params) response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None) except openerp.exceptions.AccessError, e: - fault = xmlrpclib.Fault(5, str(e)) + fault = xmlrpclib.Fault(XML_RPC_FAULT_CODE_ACCESS_ERROR, str(e)) response = xmlrpclib.dumps(fault, allow_none=False, encoding=None) except openerp.exceptions.Warning, e: - fault = xmlrpclib.Fault(4, str(e)) + fault = xmlrpclib.Fault(XML_RPC_FAULT_CODE_WARNING, str(e)) response = xmlrpclib.dumps(fault, allow_none=False, encoding=None) except openerp.exceptions.AccessDenied, e: - fault = xmlrpclib.Fault(3, str(e)) + fault = xmlrpclib.Fault(XML_RPC_FAULT_CODE_ACCESS_DENIED, str(e)) response = xmlrpclib.dumps(fault, allow_none=False, encoding=None) except openerp.netsvc.OpenERPDispatcherException, e: # TODO collapse this case with the next one.