exceptions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """The Odoo Exceptions module defines a few core exception types.
  4. Those types are understood by the RPC layer.
  5. Any other exception type bubbling until the RPC layer will be
  6. treated as a 'Server error'.
  7. .. note::
  8. If you consider introducing new exceptions,
  9. check out the :mod:`odoo.addons.test_exceptions` module.
  10. """
  11. import warnings
  12. class UserError(Exception):
  13. """Generic error managed by the client.
  14. Typically when the user tries to do something that has no sense given the current
  15. state of a record. Semantically comparable to the generic 400 HTTP status codes.
  16. """
  17. def __init__(self, message):
  18. """
  19. :param message: exception message and frontend modal content
  20. """
  21. super().__init__(message)
  22. @property
  23. def name(self):
  24. warnings.warn(
  25. "UserError attribute 'name' is a deprecated alias to args[0]",
  26. DeprecationWarning)
  27. return self.args[0]
  28. class RedirectWarning(Exception):
  29. """ Warning with a possibility to redirect the user instead of simply
  30. displaying the warning message.
  31. :param str message: exception message and frontend modal content
  32. :param int action_id: id of the action where to perform the redirection
  33. :param str button_text: text to put on the button that will trigger
  34. the redirection.
  35. :param dict additional_context: parameter passed to action_id.
  36. Can be used to limit a view to active_ids for example.
  37. """
  38. def __init__(self, message, action, button_text, additional_context=None):
  39. super().__init__(message, action, button_text, additional_context)
  40. # using this RedirectWarning won't crash if used as an UserError
  41. @property
  42. def name(self):
  43. warnings.warn(
  44. "RedirectWarning attribute 'name' is a deprecated alias to args[0]",
  45. DeprecationWarning)
  46. return self.args[0]
  47. class AccessDenied(UserError):
  48. """Login/password error.
  49. .. note::
  50. No traceback.
  51. .. admonition:: Example
  52. When you try to log with a wrong password.
  53. """
  54. def __init__(self, message="Access Denied"):
  55. super().__init__(message)
  56. self.with_traceback(None)
  57. self.__cause__ = None
  58. self.traceback = ('', '', '')
  59. class AccessError(UserError):
  60. """Access rights error.
  61. .. admonition:: Example
  62. When you try to read a record that you are not allowed to.
  63. """
  64. class CacheMiss(KeyError):
  65. """Missing value(s) in cache.
  66. .. admonition:: Example
  67. When you try to read a value in a flushed cache.
  68. """
  69. def __init__(self, record, field):
  70. super().__init__("%r.%s" % (record, field.name))
  71. class MissingError(UserError):
  72. """Missing record(s).
  73. .. admonition:: Example
  74. When you try to write on a deleted record.
  75. """
  76. class ValidationError(UserError):
  77. """Violation of python constraints.
  78. .. admonition:: Example
  79. When you try to create a new user with a login which already exist in the db.
  80. """
  81. # Deprecated exceptions, only kept for backward compatibility, may be
  82. # removed in the future *without* any further notice than the Deprecation
  83. # Warning.
  84. class except_orm(UserError):
  85. def __init__(self, name, value=None):
  86. warnings.warn("except_orm is a deprecated alias to UserError.", DeprecationWarning)
  87. super().__init__(f"{name}: {value}")
  88. class Warning(UserError):
  89. def __init__(self, *args, **kwargs):
  90. warnings.warn("Warning is a deprecated alias to UserError.", DeprecationWarning)
  91. super().__init__(*args, **kwargs)