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/share/wizard/share_wizard.py | |
| 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/share/wizard/share_wizard.py')
| -rw-r--r-- | addons/share/wizard/share_wizard.py | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/addons/share/wizard/share_wizard.py b/addons/share/wizard/share_wizard.py index f48ad7654e7..161cbfba112 100644 --- a/addons/share/wizard/share_wizard.py +++ b/addons/share/wizard/share_wizard.py @@ -33,6 +33,7 @@ from openerp.osv import expression from openerp.tools.translate import _ from openerp.tools.safe_eval import safe_eval import openerp +from openerp.exceptions import UserError _logger = logging.getLogger(__name__) FULL_ACCESS = ('perm_read', 'perm_write', 'perm_create', 'perm_unlink') @@ -58,7 +59,7 @@ class share_wizard(osv.TransientModel): The error_message should have been translated with _(). """ if not condition: - raise osv.except_osv(_('Sharing access cannot be created.'), error_message) + raise UserError(error_message) def has_group(self, cr, uid, module, group_xml_id, context=None): """Returns True if current user is a member of the group identified by the module, group_xml_id pair.""" @@ -201,8 +202,7 @@ class share_wizard(osv.TransientModel): def go_step_1(self, cr, uid, ids, context=None): wizard_data = self.browse(cr,uid,ids,context)[0] if wizard_data.user_type == 'emails' and not self.has_email(cr, uid, context=context): - raise osv.except_osv(_('No email address configured'), - _('You must configure your email address in the user preferences before using the Share button.')) + raise UserError(_('You must configure your email address in the user preferences before using the Share button.')) model, res_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'share', 'action_share_wizard_step1') action = self.pool[model].read(cr, uid, [res_id], context=context)[0] action['res_id'] = ids[0] @@ -631,9 +631,8 @@ class share_wizard(osv.TransientModel): group_id, model_id=model.id, domain=str(related_domain), rule_name=rule_name, restrict=True, context=context) except Exception: - _logger.exception('Failed to create share access') - raise osv.except_osv(_('Sharing access cannot be created.'), - _('Sorry, the current screen and filter you are trying to share are not supported at the moment.\nYou may want to try a simpler filter.')) + _logger.info('Failed to create share access', exc_info=True) + raise UserError(_('Sorry, the current screen and filter you are trying to share are not supported at the moment.\nYou may want to try a simpler filter.')) def _check_preconditions(self, cr, uid, wizard_data, context=None): self._assert(wizard_data.action_id and wizard_data.access_mode, @@ -755,7 +754,7 @@ class share_wizard(osv.TransientModel): res_id = cond[2] # Record id not found: issue if res_id <= 0: - raise osv.except_osv(_('Record id not found'), _('The share engine has not been able to fetch a record_id for your invitation.')) + raise UserError(_('The share engine has not been able to fetch a record_id for your invitation.')) self.pool[model.model].message_subscribe(cr, uid, [res_id], new_ids + existing_ids, context=context) # self.send_invite_email(cr, uid, wizard_data, context=context) # self.send_invite_note(cr, uid, model.model, res_id, wizard_data, context=context) @@ -810,7 +809,7 @@ class share_wizard(osv.TransientModel): notification_obj = self.pool.get('mail.notification') user = self.pool.get('res.users').browse(cr, UID_ROOT, uid) if not user.email: - raise osv.except_osv(_('Email Required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.')) + raise UserError(_('The current user must have an email address configured in User Preferences to be able to send outgoing emails.')) # TODO: also send an HTML version of this mail for result_line in wizard_data.result_line_ids: @@ -841,7 +840,7 @@ class share_wizard(osv.TransientModel): mail_mail = self.pool.get('mail.mail') user = self.pool.get('res.users').browse(cr, UID_ROOT, uid) if not user.email: - raise osv.except_osv(_('Email Required'), _('The current user must have an email address configured in User Preferences to be able to send outgoing emails.')) + raise UserError(_('The current user must have an email address configured in User Preferences to be able to send outgoing emails.')) # TODO: also send an HTML version of this mail mail_ids = [] |
