[ADD] added serialize_exception decorator

See server's trunk-website-al revision #5085

bzr revid: fme@openerp.com-20131128181016-2t8d0qcrmux9nkoe
This commit is contained in:
Fabien Meghazi
2013-11-28 19:10:16 +01:00
parent e468eeb9b3
commit f4c0fac4fb
+27 -1
View File
@@ -3,6 +3,7 @@
import ast
import base64
import csv
import functools
import glob
import itertools
import logging
@@ -35,7 +36,9 @@ from openerp.tools.translate import _
from openerp.tools import config
from openerp import http
from openerp.http import request
from openerp.http import request, serialize_exception as _serialize_exception
_logger = logging.getLogger(__name__)
#----------------------------------------------------------
# OpenERP Web helpers
@@ -89,6 +92,22 @@ db_list = http.db_list
db_monodb = http.db_monodb
def serialize_exception(f):
@functools.wraps(f)
def wrap(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception, e:
_logger.exception("An exception occured during an http request")
se = _serialize_exception(e)
error = {
'code': 200,
'message': "OpenERP Server Error",
'data': se
}
return werkzeug.exceptions.InternalServerError(simplejson.dumps(error))
return wrap
def redirect_with_hash(url, code=303):
redirect_code = "<html><head><script>window.location = '%s' + location.hash;</script></head></html>" % url
if request.httprequest.user_agent.browser == 'msie':
@@ -1225,6 +1244,7 @@ class Binary(http.Controller):
return open(os.path.join(addons_path, 'web', 'static', 'src', 'img', image), 'rb').read()
@http.route('/web/binary/saveas', type='http', auth="user")
@serialize_exception
def saveas(self, model, field, id=None, filename_field=None, **kw):
""" Download link for files stored as binary fields.
@@ -1258,6 +1278,7 @@ class Binary(http.Controller):
('Content-Disposition', content_disposition(filename))])
@http.route('/web/binary/saveas_ajax', type='http', auth="user")
@serialize_exception
def saveas_ajax(self, data, token):
jdata = simplejson.loads(data)
model = jdata['model']
@@ -1291,6 +1312,7 @@ class Binary(http.Controller):
cookies={'fileToken': token})
@http.route('/web/binary/upload', type='http', auth="user")
@serialize_exception
def upload(self, callback, ufile):
# TODO: might be useful to have a configuration flag for max-length file uploads
out = """<script language="javascript" type="text/javascript">
@@ -1306,6 +1328,7 @@ class Binary(http.Controller):
return out % (simplejson.dumps(callback), simplejson.dumps(args))
@http.route('/web/binary/upload_attachment', type='http', auth="user")
@serialize_exception
def upload_attachment(self, callback, model, id, ufile):
Model = request.session.model('ir.attachment')
out = """<script language="javascript" type="text/javascript">
@@ -1595,6 +1618,7 @@ class ExportFormat(object):
class CSVExport(ExportFormat, http.Controller):
@http.route('/web/export/csv', type='http', auth="user")
@serialize_exception
def index(self, data, token):
return self.base(data, token)
@@ -1632,6 +1656,7 @@ class CSVExport(ExportFormat, http.Controller):
class ExcelExport(ExportFormat, http.Controller):
@http.route('/web/export/xls', type='http', auth="user")
@serialize_exception
def index(self, data, token):
return self.base(data, token)
@@ -1678,6 +1703,7 @@ class Reports(http.Controller):
}
@http.route('/web/report', type='http', auth="user")
@serialize_exception
def index(self, action, token):
action = simplejson.loads(action)