[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.
This commit is contained in:
Olivier Dony
2016-06-02 00:50:36 +02:00
parent 57944db19a
commit ef0d05b6e0
+6 -1
View File
@@ -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]