From ab4000fb3cf8fa54ba714ceceec391b78d73755e Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Wed, 19 Feb 2020 13:34:27 +0000 Subject: [REF] base: Remove deprecated exceptions and osv TL;DR: remember `osv` and `except_orm` ? You can forget about them. * Deprecated `except_orm` dropped. * `UserError` elevated as super type of all user-related errors. * Unused `DeferredException` dropped. * Unused `QWebException` dropped (real one is in `qweb.py`). * `MailDeliveryException` made a python exception. * `name` legacy exception attribute made an alias of the python standard `args[0]` attribute and deprecated. * `value` legacy exception attribute dropped. * `exception_type` RPC error response key dropped. * Deprecated `osv` module dropped. * `--osv-memory-age-limit` cli option made an alias of `--transient-age-limit` and deprecated. The `odoo.exceptions.Warning` have long been a deprecated alias to `UserError`. It is going to be removed in a future version but first we explicitly deprecate it with a warning. The `odoo.exceptions.DeferredException` was a very old internal exception, it has been removed without deprecation notice as it is never raised. The `odoo.exceptions.except_orm` has been a deprecated exception type with deprecation warning for 5 years, it has been removed in favor of UserError which becomes the super class of all user-related errors. The `odoo.base.models.ir_mail_server.MailDeliveryException` was inheriting `except_orm`. As it is not related to a user error but is more of a problem an admin much take care of, the exception has been made a Python error. The `exception_type` JSON key in RPC error responses was holding an hardcoded value derived from the exception type. Its usage has been dropped in favor of the `name` JSON key that holds the precise exception name. Again as it was hardly used in the source code (beside the crash manager) it has been dropped without deprecation warning. Since we are here trying to clean odoo custom exceptions, we are also deprecating the `name` exception attribute in favor of the more standard `args[0]` attribute. The `name` (along with `value`) were two attributes used to raise `except_orm` exceptions before the introduction of `UserError`, `AccessError` and related exceptions. The `name` attribute, at the time, was holding the exception type/title. Nowadays it contains the error message. The `value` attribute, at the time, was holding the error message. Nowadays it is no more used. The `osv` module contains very old deprecated aliases. There is no simple way to log a deprecation warning for osv, osv_memory and osv_abstract but as they have not been in use for ages, they have been removed too. To be consistent, the `--osv-memory-age-limit` cli option has been made a deprecated alias to the `--transient-age-limit`. closes odoo/odoo#45723 Task: 2187728 Related: odoo/enterprise#9162 Signed-off-by: Raphael Collet (rco) --- odoo/exceptions.py | 100 +++++++++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 61 deletions(-) (limited to 'odoo/exceptions.py') diff --git a/odoo/exceptions.py b/odoo/exceptions.py index 93a0a84e20f..8400194e75a 100644 --- a/odoo/exceptions.py +++ b/odoo/exceptions.py @@ -13,58 +13,64 @@ treated as a 'Server error'. """ import logging -from inspect import currentframe -from .tools.func import frame_codeinfo +import warnings _logger = logging.getLogger(__name__) -# kept for backward compatibility -class except_orm(Exception): - def __init__(self, name, value=None): - if type(self) == except_orm: - caller = frame_codeinfo(currentframe(), 1) - _logger.warning('except_orm is deprecated. Please use specific exceptions like UserError or AccessError. Caller: %s:%s', *caller) - self.name = name - self.value = value - self.args = (name, value) - - def __str__(self): - if not self.value: - return str(self.name) - else: - return super().__str__() - - -class UserError(except_orm): +class UserError(Exception): """Generic error managed by the client. Typically when the user tries to do something that has no sense given the current - state of a record. + state of a record. Semantically comparable to the generic 400 HTTP status codes. """ - def __init__(self, msg): - super(UserError, self).__init__(msg, value='') + + def __init__(self, message): + """ + :param message: exception message and frontend modal content + """ + super().__init__(message) + + @property + def name(self): + warnings.warn( + "UserError attribute 'name' is a deprecated alias to args[0]", + DeprecationWarning) + return self.args[0] # deprecated due to collision with builtins, kept for compatibility -Warning = UserError +class Warning(UserError): + def __init__(self, *args, **kwargs): + warnings.warn( + "Warning is a deprecated alias to UserError and is going to be " + "removed in a future version.", + DeprecationWarning) + super().__init__(*args, **kwargs) class RedirectWarning(Exception): """ Warning with a possibility to redirect the user instead of simply displaying the warning message. + :param str message: exception message and frontend modal content :param int action_id: id of the action where to perform the redirection :param str button_text: text to put on the button that will trigger the redirection. """ - # using this RedirectWarning won't crash if used as an except_orm + def __init__(self, message, action, button_text): + super().__init__(message, action, button_text) + + # using this RedirectWarning won't crash if used as an UserError @property def name(self): + warnings.warn( + "RedirectWarning attribute 'name' is a deprecated alias to args[0]", + DeprecationWarning) return self.args[0] -class AccessDenied(Exception): +class AccessDenied(UserError): """Login/password error. .. note:: @@ -76,14 +82,14 @@ class AccessDenied(Exception): When you try to log with a wrong password. """ - def __init__(self, message='Access denied'): - super(AccessDenied, self).__init__(message) + def __init__(self, message="Access Denied"): + super().__init__(message) self.with_traceback(None) self.__cause__ = None self.traceback = ('', '', '') -class AccessError(except_orm): +class AccessError(UserError): """Access rights error. .. admonition:: Example @@ -91,11 +97,8 @@ class AccessError(except_orm): When you try to read a record that you are not allowed to. """ - def __init__(self, msg): - super(AccessError, self).__init__(msg) - -class CacheMiss(except_orm, KeyError): +class CacheMiss(KeyError): """Missing value(s) in cache. .. admonition:: Example @@ -104,10 +107,10 @@ class CacheMiss(except_orm, KeyError): """ def __init__(self, record, field): - super(CacheMiss, self).__init__("%r.%s" % (record, field.name)) + super().__init__("%r.%s" % (record, field.name)) -class MissingError(except_orm): +class MissingError(UserError): """Missing record(s). .. admonition:: Example @@ -115,36 +118,11 @@ class MissingError(except_orm): When you try to write on a deleted record. """ - def __init__(self, msg): - super(MissingError, self).__init__(msg) - -class ValidationError(except_orm): +class ValidationError(UserError): """Violation of python constraints. .. admonition:: Example When you try to create a new user with a login which already exist in the db. """ - - def __init__(self, msg): - super(ValidationError, self).__init__(msg) - - -class DeferredException(Exception): - """ Exception object holding a traceback for asynchronous reporting. - - Some RPC calls (database creation and report generation) happen with - an initial request followed by multiple, polling requests. This class - is used to store the possible exception occuring in the thread serving - the first request, and is then sent to a polling request. - - ('Traceback' is misleading, this is really a exc_info() triple.) - """ - def __init__(self, msg, tb): - self.message = msg - self.traceback = tb - - -class QWebException(Exception): - pass -- cgit v1.2.3