summaryrefslogtreecommitdiff
path: root/openerp/sql_db.py
diff options
context:
space:
mode:
authorGoffin Simon <simon.goffin@outlook.com>2014-12-18 16:38:09 +0100
committerChristophe Simonis <chs@odoo.com>2015-01-16 17:15:18 +0100
commit0fd773a486fbc1aeef7f00ec5eab3eaf0e4c6a9d (patch)
treee0b6ab3c1d7c21a2539235805912ee82efd4f23a /openerp/sql_db.py
parenteeb8ed165d713f41d7cde33e46acd7accef722fb (diff)
[IMP] Cleanup and refactoring of exception handling
Unify and refactor exception handling in framework and addons. The generic `except_osv` is now deprecated, and replaced by more specialized exception subtypes: - `UserError` (renamed from Warning, as it conflicts with the built-in `Warning`) raised when a non-technical error occurs during a business operation. It could be a missing information in the data provided by the user, or a misconfiguration. - `AccessError`: raised when any operation is denied because the user conducting it does not have the required access rights. - `AccessDenied`: raised when an operation that requires authenticated access is attempted via an unauthenticated request. - `MissingError`: raised when an operation is attempted on a record that does not exist. - `ValidationError`: raised when an operation violates a SQL or Python constraint. - All other exceptions are internal errors due to a system problem or bug, and raised untouched to the client-side, which should display a traceback. All exceptions take a single message argument. The `test_exceptions` module has been updated to showcase both new and old (deprecated) exceptions. A great many old `except_osv` had a useless title with "Error!" or "Warning", those have been removed, as this is handled by the client-side widget that displays the messages. This commit introduces a more consistent policy for logging errors and warnings: - All messages that do not require administrator attention should be logged at INFO level or lower. This includes all errors that are notified to the user in a friendly manner, even for access right problems or validation errors during business operations. - All messages that indicate a likely misconfiguration or malicious use by the users should be logged at WARNING level, as they typically require administrator attention. - All other unhandled internal errors cannot typically be handled by the user and should be logged at ERROR or higher level, as they require immediate administrator attention.
Diffstat (limited to 'openerp/sql_db.py')
-rw-r--r--openerp/sql_db.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/openerp/sql_db.py b/openerp/sql_db.py
index 52c8c21239e..3f9cf236965 100644
--- a/openerp/sql_db.py
+++ b/openerp/sql_db.py
@@ -214,16 +214,16 @@ class Cursor(object):
msg += "Cursor was created at %s:%s" % self.__caller
else:
msg += "Please enable sql debugging to trace the caller."
- _logger.warning(msg)
+ _logger.info(msg)
self._close(True)
@check
def execute(self, query, params=None, log_exceptions=None):
if '%d' in query or '%f' in query:
- _logger.warning(query)
- _logger.warning("SQL queries cannot contain %d or %f anymore. Use only %s")
+ _logger.info("SQL queries cannot contain %%d or %%f anymore. Use only %%s:\n%s" % query,
+ exc_info=_logger.isEnabledFor(logging.DEBUG))
if params and not isinstance(params, (tuple, list, dict)):
- _logger.error("SQL query parameters should be a tuple, list or dict; got %r", params)
+ _logger.info("SQL query parameters should be a tuple, list or dict; got %r", params)
raise ValueError("SQL query parameters should be a tuple, list or dict; got %r" % (params,))
if self.sql_log:
@@ -234,11 +234,11 @@ class Cursor(object):
res = self._obj.execute(query, params)
except psycopg2.ProgrammingError, pe:
if self._default_log_exceptions if log_exceptions is None else log_exceptions:
- _logger.error("Programming error: %s, in query %s", pe, query)
+ _logger.info("Programming error: %s, in query %s", pe, query)
raise
except Exception:
if self._default_log_exceptions if log_exceptions is None else log_exceptions:
- _logger.exception("bad query: %s", self._obj.query or query)
+ _logger.info("bad query: %s", self._obj.query or query)
raise
# simple query count is always computed
@@ -487,7 +487,7 @@ class ConnectionPool(object):
delattr(cnx, 'leaked')
self._connections.pop(i)
self._connections.append((cnx, False))
- _logger.warning('%r: Free leaked connection to %r', self, cnx.dsn)
+ _logger.info('%r: Free leaked connection to %r', self, cnx.dsn)
for i, (cnx, used) in enumerate(self._connections):
if not used and cnx._original_dsn == dsn:
@@ -521,7 +521,7 @@ class ConnectionPool(object):
try:
result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
except psycopg2.Error:
- _logger.exception('Connection to the database failed')
+ _logger.info('Connection to the database failed')
raise
result._original_dsn = dsn
self._connections.append((result, True))
@@ -581,7 +581,7 @@ class Connection(object):
def __nonzero__(self):
"""Check if connection is possible"""
try:
- _logger.warning("__nonzero__() is deprecated. (It is too expensive to test a connection.)")
+ _logger.info("__nonzero__() is deprecated. (It is too expensive to test a connection.)")
cr = self.cursor()
cr.close()
return True