From ef0d05b6e00736e6bb69b4f0e5a849f4d8246657 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 1 Jun 2016 12:31:57 +0200 Subject: [PATCH] [IMP] safe_eval: let AuthenticationError and werkzeug HTTPExceptions bubble up - Werkzeug's HTTPException are useful while dispatching HTTP requests, in order to return a custom error page with an appropriate status code. The http stack should let those exceptions bubble all the way up and return the body of the exception as the HTTP response. - openerp.http.AuthenticationError may be useful while dispatching JSON requests, because the JSON error result will have error code 100 instead of the usual code 200, making it easy to distinguish in simple JSON client code. - Also slightly improved the fallback for unknown exceptions occurring during "safe evaluation", to explicitly mention the original exception type in the generic ValueError that gets raised. --- openerp/tools/safe_eval.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openerp/tools/safe_eval.py b/openerp/tools/safe_eval.py index a2f5e848c12..ffbf7e46ffd 100644 --- a/openerp/tools/safe_eval.py +++ b/openerp/tools/safe_eval.py @@ -19,6 +19,7 @@ from opcode import HAVE_ARGUMENT, opmap, opname from psycopg2 import OperationalError from types import CodeType import logging +import werkzeug from .misc import ustr @@ -306,6 +307,10 @@ def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=Fal raise except openerp.exceptions.AccessError: raise + except werkzeug.exceptions.HTTPException: + raise + except openerp.http.AuthenticationError: + raise except OperationalError: # Do not hide PostgreSQL low-level exceptions, to let the auto-replay # of serialized transactions work its magic @@ -315,4 +320,4 @@ def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=Fal except Exception, e: import sys exc_info = sys.exc_info() - raise ValueError, '"%s" while evaluating\n%r' % (ustr(e), expr), exc_info[2] + raise ValueError, '%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr), exc_info[2]