summaryrefslogtreecommitdiff
path: root/addons/gamification
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 /addons/gamification
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 'addons/gamification')
-rw-r--r--addons/gamification/models/badge.py9
-rw-r--r--addons/gamification/models/challenge.py5
-rw-r--r--addons/gamification/models/goal.py5
-rw-r--r--addons/gamification/wizard/grant_badge.py3
4 files changed, 13 insertions, 9 deletions
diff --git a/addons/gamification/models/badge.py b/addons/gamification/models/badge.py
index c078943ee0e..c21595a5d0d 100644
--- a/addons/gamification/models/badge.py
+++ b/addons/gamification/models/badge.py
@@ -26,6 +26,7 @@ from openerp.tools.translate import _
from datetime import date
import logging
+from openerp.exceptions import UserError
_logger = logging.getLogger(__name__)
@@ -235,13 +236,13 @@ class gamification_badge(osv.Model):
if status_code == self.CAN_GRANT:
return True
elif status_code == self.NOBODY_CAN_GRANT:
- raise osv.except_osv(_('Warning!'), _('This badge can not be sent by users.'))
+ raise UserError(_('This badge can not be sent by users.'))
elif status_code == self.USER_NOT_VIP:
- raise osv.except_osv(_('Warning!'), _('You are not in the user allowed list.'))
+ raise UserError(_('You are not in the user allowed list.'))
elif status_code == self.BADGE_REQUIRED:
- raise osv.except_osv(_('Warning!'), _('You do not have the required badges.'))
+ raise UserError(_('You do not have the required badges.'))
elif status_code == self.TOO_MANY:
- raise osv.except_osv(_('Warning!'), _('You have already sent this badge too many time this month.'))
+ raise UserError(_('You have already sent this badge too many time this month.'))
else:
_logger.exception("Unknown badge status code: %d" % int(status_code))
return False
diff --git a/addons/gamification/models/challenge.py b/addons/gamification/models/challenge.py
index a804d816a05..22f054eb7a9 100644
--- a/addons/gamification/models/challenge.py
+++ b/addons/gamification/models/challenge.py
@@ -28,6 +28,7 @@ from openerp.tools.translate import _
from datetime import date, datetime, timedelta
import calendar
import logging
+from openerp.exceptions import UserError
_logger = logging.getLogger(__name__)
# display top 3 in ranking, could be db variable
@@ -248,7 +249,7 @@ class gamification_challenge(osv.Model):
elif vals.get('state') == 'draft':
# resetting progress
if self.pool.get('gamification.goal').search(cr, uid, [('challenge_id', 'in', ids), ('state', '=', 'inprogress')], context=context):
- raise osv.except_osv("Error", "You can not reset a challenge with unfinished goals.")
+ raise UserError(_("You can not reset a challenge with unfinished goals."))
return write_res
@@ -553,7 +554,7 @@ class gamification_challenge(osv.Model):
if challenge.visibility_mode == 'personal':
if not user_id:
- raise osv.except_osv(_('Error!'),_("Retrieving progress for personal challenge without user information"))
+ raise UserError(_("Retrieving progress for personal challenge without user information"))
domain.append(('user_id', '=', user_id))
sorting = goal_obj._order
limit = 1
diff --git a/addons/gamification/models/goal.py b/addons/gamification/models/goal.py
index 415befbc8e8..48adf095ac5 100644
--- a/addons/gamification/models/goal.py
+++ b/addons/gamification/models/goal.py
@@ -24,6 +24,7 @@ from openerp.osv import fields, osv
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp.tools.safe_eval import safe_eval
from openerp.tools.translate import _
+from openerp.exceptions import UserError
import logging
import time
@@ -145,7 +146,7 @@ class gamification_goal_definition(osv.Model):
obj.search(cr, uid, domain, context=context, count=True)
except (ValueError, SyntaxError), e:
msg = e.message or (e.msg + '\n' + e.text)
- raise osv.except_osv(_('Error!'),_("The domain for the definition %s seems incorrect, please check it.\n\n%s" % (definition.name, msg)))
+ raise UserError(_("The domain for the definition %s seems incorrect, please check it.\n\n%s" % (definition.name, msg)))
return True
def create(self, cr, uid, vals, context=None):
@@ -450,7 +451,7 @@ class gamification_goal(osv.Model):
for goal in self.browse(cr, uid, ids, context=context):
if goal.state != "draft" and ('definition_id' in vals or 'user_id' in vals):
# avoid drag&drop in kanban view
- raise osv.except_osv(_('Error!'), _('Can not modify the configuration of a started goal'))
+ raise UserError(_('Can not modify the configuration of a started goal'))
if vals.get('current'):
if 'no_remind_goal' in context:
diff --git a/addons/gamification/wizard/grant_badge.py b/addons/gamification/wizard/grant_badge.py
index 513a1526c8b..b826bc4e563 100644
--- a/addons/gamification/wizard/grant_badge.py
+++ b/addons/gamification/wizard/grant_badge.py
@@ -21,6 +21,7 @@
from openerp.osv import fields, osv
from openerp.tools.translate import _
+from openerp.exceptions import UserError
class grant_badge_wizard(osv.TransientModel):
@@ -40,7 +41,7 @@ class grant_badge_wizard(osv.TransientModel):
for wiz in self.browse(cr, uid, ids, context=context):
if uid == wiz.user_id.id:
- raise osv.except_osv(_('Warning!'), _('You can not grant a badge to yourself'))
+ raise UserError(_('You can not grant a badge to yourself'))
#create the badge
values = {