From 44384e8688d25924ef025bde423707031e92f580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 26 Aug 2008 20:10:45 +0200 Subject: [PATCH] Add error message from orm on IntegrityError --- trytond/osv/osv.py | 56 ++++++++++++++++++---------------------- trytond/report/report.py | 24 +++++++---------- trytond/wizard/wizard.py | 24 +++++++---------- 3 files changed, 45 insertions(+), 59 deletions(-) diff --git a/trytond/osv/osv.py b/trytond/osv/osv.py index f0052f6bc5..9934c2c789 100644 --- a/trytond/osv/osv.py +++ b/trytond/osv/osv.py @@ -29,6 +29,7 @@ class OSVService(Service): Service.export_method(self, self.exec_workflow) Service.export_method(self, self.execute) Service.export_method(self, self.execute_cr) + self._sql_errors = {} def execute_cr(self, cursor, user, object_name, method, *args, **kargs): # TODO: check security level @@ -44,22 +45,17 @@ class OSVService(Service): res = getattr(obj, method)(cursor, user, *args, **kargs) return res except Exception, exception: - if exception.args \ - and exception.args[0] in ('UserError', - 'ConcurrencyException') \ - and not CONFIG['verbose']: - raise - if isinstance(exception, IntegrityError) and not CONFIG['verbose']: - raise Exception('UserError', 'Constraint Error', - *exception.args) - tb_s = reduce(lambda x, y: x+y, - traceback.format_exception(*sys.exc_info())) - logger = Logger() - logger.notify_channel("web-service", LOG_ERROR, - 'Exception in call: \n' + tb_s) + if CONFIG['verbose']: + tb_s = reduce(lambda x, y: x+y, + traceback.format_exception(*sys.exc_info())) + logger = Logger() + logger.notify_channel("web-service", LOG_ERROR, + 'Exception in call: \n' + tb_s) if isinstance(exception, IntegrityError): - raise Exception('UserError', 'Constraint Error', - *exception.args) + for key in self._sql_errors.keys(): + if key in exception[0]: + raise Exception('UserError', 'Constraint Error', + self._sql_errors[key]) raise def execute(self, dbname, user, object_name, method, *args, **kargs): @@ -83,22 +79,17 @@ class OSVService(Service): try: wf_service.trg_validate(user, object_name, args[0], method, cursor) except Exception, exception: - if exception.args \ - and exception.args[0] in ('UserError', - 'ConcurrencyException') \ - and not CONFIG['verbose']: - raise - if isinstance(exception, IntegrityError) and not CONFIG['verbose']: - raise Exception('UserError', 'Constraint Error', - *exception.args) - tb_s = reduce(lambda x, y: x+y, - traceback.format_exception(*sys.exc_info())) - logger = Logger() - logger.notify_channel("web-service", LOG_ERROR, - 'Exception in call: \n' + tb_s) + if CONFIG['verbose']: + tb_s = reduce(lambda x, y: x+y, + traceback.format_exception(*sys.exc_info())) + logger = Logger() + logger.notify_channel("web-service", LOG_ERROR, + 'Exception in call: \n' + tb_s) if isinstance(exception, IntegrityError): - raise Exception('UserError', 'Constraint Error', - *exception.args) + for key in self._sql_errors.keys(): + if key in exception[0]: + raise Exception('UserError', 'Constraint Error', + self._sql_errors[key]) raise return True @@ -141,7 +132,10 @@ class OSVService(Service): res = [] class_list = MODULE_CLASS_LIST.get(module, []) for klass in class_list: - res.append(klass.create_instance(self, module)) + inst = klass.create_instance(self, module) + res.append(inst) + for key, _, msg in inst._sql_constraints: + self._sql_errors[inst._table + '_' + key] = msg return res osv_pool = OSVService diff --git a/trytond/report/report.py b/trytond/report/report.py index 04b59cba15..91b2155a90 100644 --- a/trytond/report/report.py +++ b/trytond/report/report.py @@ -190,21 +190,17 @@ class ReportService(Service): res = report.execute(cursor, user, ids, datas, context) return res except Exception, exception: - if exception.args \ - and exception.args[0] in ('UserError', - 'ConcurrencyException') \ - and not CONFIG['verbose']: - raise - if isinstance(exception, IntegrityError) and not CONFIG['verbose']: - raise Exception('UserError', 'Constraint Error', - *exception.args) - tb_s = reduce(lambda x, y: x+y, - traceback.format_exception(*sys.exc_info())) - Logger().notify_channel("web-service", LOG_ERROR, - 'Exception in call: ' + tb_s) + if CONFIG['verbose']: + tb_s = reduce(lambda x, y: x+y, + traceback.format_exception(*sys.exc_info())) + Logger().notify_channel("web-service", LOG_ERROR, + 'Exception in call: ' + tb_s) if isinstance(exception, IntegrityError): - raise Exception('UserError', 'Constraint Error', - *exception.args) + pool = pooler.get_pool(cursor.dbname) + for key in pool._sql_errors.keys(): + if key in exception[0]: + raise Exception('UserError', 'Constraint Error', + pool._sql_errors[key]) raise def execute(self, dbname, user, report_name, ids, datas, context=None): diff --git a/trytond/wizard/wizard.py b/trytond/wizard/wizard.py index 1bb5c32a84..48b0083512 100644 --- a/trytond/wizard/wizard.py +++ b/trytond/wizard/wizard.py @@ -34,21 +34,17 @@ class WizardService(Service): res = wizard.execute(cursor, user, data, state, context) return res except Exception, exception: - if exception.args \ - and exception.args[0] in ('UserError', - 'ConcurrencyException') \ - and not CONFIG['verbose']: - raise - if isinstance(exception, IntegrityError) and not CONFIG['verbose']: - raise Exception('UserError', 'Constraint Error', - *exception.args) - tb_s = reduce(lambda x, y: x+y, - traceback.format_exception(*sys.exc_info())) - Logger().notify_channel("web-service", LOG_ERROR, - 'Exception in call: ' + tb_s) + if CONFIG['verbose']: + tb_s = reduce(lambda x, y: x+y, + traceback.format_exception(*sys.exc_info())) + Logger().notify_channel("web-service", LOG_ERROR, + 'Exception in call: ' + tb_s) if isinstance(exception, IntegrityError): - raise Exception('UserError', 'Constraint Error', - *exception.args) + pool = pooler.get_pool(cursor.dbname) + for key in pool._sql_errors.keys(): + if key in exception[0]: + raise Exception('UserError', 'Constraint Error', + pool._sql_errors[key]) raise def execute(self, dbname, user, wizard_name, data, state='init',