From 07d5ea3779d796f621ce4cc5f9c2caa940d2012e Mon Sep 17 00:00:00 2001 From: Cedric Snauwaert Date: Tue, 18 Feb 2020 12:53:00 +0000 Subject: [PATCH] [FIX] http.py: remove try except in JsonRequest.dispatch() The idea is to make the dispatching and error-handling consistent for JsonRequest and HTTPRequest. In 8809c77f6088c140355fba5f862c0335b1d90c4e we introduced a way for request-specific error-handling, but JsonRequest.dispatch() was still catching all errors internally, instead of letting them bubble up to ir_http._handle_exception() By removing the internal try except in JsonRequest.dispatch(), we do not change the behavior much as there are only a couple of "if" in ir_http._handle_exception() before returning to the request-specific handle_exception. However we give the opportunity to modules to customize exception handling even for JsonRPC, the same way it is possible for HTTP requests. Here is a simple pseudo code of the flow ir_http.dispatch(): try: request.dispatch() catch Exception as e: ir_http._handle_exception(e) ir_http.handle_exception(e) request._handle_exception(e) closes odoo/odoo#45639 Signed-off-by: Olivier Dony (odo) --- odoo/http.py | 58 ++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/odoo/http.py b/odoo/http.py index 4c0cf76005c..3b2a6468cf3 100644 --- a/odoo/http.py +++ b/odoo/http.py @@ -606,7 +606,6 @@ class JsonRequest(WebRequest): self.context = self.params.pop('context', dict(self.session.context)) def _json_response(self, result=None, error=None): - response = { 'jsonrpc': '2.0', 'id': self.jsonrequest.get('id') @@ -657,40 +656,37 @@ class JsonRequest(WebRequest): return self._json_response(error=error) def dispatch(self): - try: - rpc_request_flag = rpc_request.isEnabledFor(logging.DEBUG) - rpc_response_flag = rpc_response.isEnabledFor(logging.DEBUG) - if rpc_request_flag or rpc_response_flag: - endpoint = self.endpoint.method.__name__ - model = self.params.get('model') - method = self.params.get('method') - args = self.params.get('args', []) + rpc_request_flag = rpc_request.isEnabledFor(logging.DEBUG) + rpc_response_flag = rpc_response.isEnabledFor(logging.DEBUG) + if rpc_request_flag or rpc_response_flag: + endpoint = self.endpoint.method.__name__ + model = self.params.get('model') + method = self.params.get('method') + args = self.params.get('args', []) - start_time = time.time() - start_memory = 0 - if psutil: - start_memory = memory_info(psutil.Process(os.getpid())) - if rpc_request and rpc_response_flag: - rpc_request.debug('%s: %s %s, %s', - endpoint, model, method, pprint.pformat(args)) + start_time = time.time() + start_memory = 0 + if psutil: + start_memory = memory_info(psutil.Process(os.getpid())) + if rpc_request and rpc_response_flag: + rpc_request.debug('%s: %s %s, %s', + endpoint, model, method, pprint.pformat(args)) - result = self._call_function(**self.params) + result = self._call_function(**self.params) - if rpc_request_flag or rpc_response_flag: - end_time = time.time() - end_memory = 0 - if psutil: - end_memory = memory_info(psutil.Process(os.getpid())) - logline = '%s: %s %s: time:%.3fs mem: %sk -> %sk (diff: %sk)' % ( - endpoint, model, method, end_time - start_time, start_memory / 1024, end_memory / 1024, (end_memory - start_memory)/1024) - if rpc_response_flag: - rpc_response.debug('%s, %s', logline, pprint.pformat(result)) - else: - rpc_request.debug(logline) + if rpc_request_flag or rpc_response_flag: + end_time = time.time() + end_memory = 0 + if psutil: + end_memory = memory_info(psutil.Process(os.getpid())) + logline = '%s: %s %s: time:%.3fs mem: %sk -> %sk (diff: %sk)' % ( + endpoint, model, method, end_time - start_time, start_memory / 1024, end_memory / 1024, (end_memory - start_memory)/1024) + if rpc_response_flag: + rpc_response.debug('%s, %s', logline, pprint.pformat(result)) + else: + rpc_request.debug(logline) - return self._json_response(result) - except Exception as e: - return self._handle_exception(e) + return self._json_response(result) def serialize_exception(e):