diff options
| author | Goffin Simon <simon.goffin@outlook.com> | 2014-12-18 16:38:09 +0100 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2015-01-16 17:15:18 +0100 |
| commit | 0fd773a486fbc1aeef7f00ec5eab3eaf0e4c6a9d (patch) | |
| tree | e0b6ab3c1d7c21a2539235805912ee82efd4f23a /addons/website_forum | |
| parent | eeb8ed165d713f41d7cde33e46acd7accef722fb (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/website_forum')
| -rw-r--r-- | addons/website_forum/models/forum.py | 8 | ||||
| -rw-r--r-- | addons/website_forum/tests/test_forum.py | 6 |
2 files changed, 7 insertions, 7 deletions
diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index 9ad2bdd49da..b8fe561d4a5 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -13,7 +13,7 @@ from openerp import modules from openerp import tools from openerp import SUPERUSER_ID from openerp.addons.website.models.website import slug -from openerp.exceptions import Warning +from openerp.exceptions import UserError _logger = logging.getLogger(__name__) @@ -297,7 +297,7 @@ class Post(models.Model): post = super(Post, self.with_context(mail_create_nolog=True)).create(vals) # deleted or closed questions if post.parent_id and (post.parent_id.state == 'close' or post.parent_id.active is False): - raise Warning(_('Posting answer on a [Deleted] or [Closed] question is not possible')) + raise UserError(_('Posting answer on a [Deleted] or [Closed] question is not possible')) # karma-based access if not post.parent_id and not post.can_ask: raise KarmaError('Not enough karma to create a new question') @@ -574,7 +574,7 @@ class Vote(models.Model): # own post check if vote.user_id.id == vote.post_id.create_uid.id: - raise Warning('Not allowed to vote for its own post') + raise UserError(_('Not allowed to vote for its own post')) # karma check if vote.vote == '1' and not vote.post_id.can_upvote: raise KarmaError('Not enough karma to upvote.') @@ -594,7 +594,7 @@ class Vote(models.Model): for vote in self: # own post check if vote.user_id.id == vote.post_id.create_uid.id: - raise Warning('Not allowed to vote for its own post') + raise UserError(_('Not allowed to vote for its own post')) # karma check if (values['vote'] == '1' or vote.vote == '-1' and values['vote'] == '0') and not vote.post_id.can_upvote: raise KarmaError('Not enough karma to upvote.') diff --git a/addons/website_forum/tests/test_forum.py b/addons/website_forum/tests/test_forum.py index 7ca4c2a69ab..50ce7055ced 100644 --- a/addons/website_forum/tests/test_forum.py +++ b/addons/website_forum/tests/test_forum.py @@ -2,7 +2,7 @@ from openerp.addons.website_forum.tests.common import KARMA, TestForumCommon from openerp.addons.website_forum.models.forum import KarmaError -from openerp.exceptions import Warning, AccessError +from openerp.exceptions import UserError, AccessError from openerp.tools import mute_logger @@ -67,7 +67,7 @@ class TestForum(TestForumCommon): 'parent_id': self.post.id}) # upvote its own post - with self.assertRaises(Warning): + with self.assertRaises(UserError): emp_answer.vote(upvote=True) # not enough karma @@ -90,7 +90,7 @@ class TestForum(TestForumCommon): 'parent_id': self.post.id}) # downvote its own post - with self.assertRaises(Warning): + with self.assertRaises(UserError): emp_answer.vote(upvote=False) # not enough karma |
