Coverage for adhoc-cicd-odoo-odoo / odoo / exceptions.py: 71%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-09 18:05 +0000

1"""The Odoo Exceptions module defines a few core exception types. 

2 

3Those types are understood by the RPC layer. 

4Any other exception type bubbling until the RPC layer will be 

5treated as a 'Server error'. 

6""" 

7 

8 

9class UserError(Exception): 

10 """Generic error managed by the client. 

11 

12 Typically when the user tries to do something that has no sense given the current 

13 state of a record. 

14 """ 

15 http_status = 422 # Unprocessable Entity 

16 

17 def __init__(self, message): 

18 """ 

19 :param message: exception message and frontend modal content 

20 """ 

21 super().__init__(message) 

22 

23 

24class RedirectWarning(Exception): 

25 """ Warning with a possibility to redirect the user instead of simply 

26 displaying the warning message. 

27 

28 :param str message: exception message and frontend modal content 

29 :param int action_id: id of the action where to perform the redirection 

30 :param str button_text: text to put on the button that will trigger 

31 the redirection. 

32 :param dict additional_context: parameter passed to action_id. 

33 Can be used to limit a view to active_ids for example. 

34 """ 

35 def __init__(self, message, action, button_text, additional_context=None): 

36 super().__init__(message, action, button_text, additional_context) 

37 

38 

39class AccessDenied(UserError): 

40 """Login/password error. 

41 

42 .. note:: 

43 

44 Traceback only visible in the logs. 

45 

46 .. admonition:: Example 

47 

48 When you try to log with a wrong password. 

49 """ 

50 http_status = 403 # Forbidden 

51 

52 def __init__(self, message="Access Denied"): 

53 super().__init__(message) 

54 self.suppress_traceback() # must be called in `except`s too 

55 

56 def suppress_traceback(self): 

57 """ 

58 Remove the traceback, cause and context of the exception, hiding 

59 where the exception occured but keeping the exception message. 

60 

61 This method must be called in all situations where we are about 

62 to print this exception to the users. 

63 

64 It is OK to leave the traceback (thus to *not* call this method) 

65 if the exception is only logged in the logs, as they are only 

66 accessible by the system administrators. 

67 """ 

68 self.with_traceback(None) 

69 self.traceback = ('', '', '') 

70 

71 # During handling of the above exception, another exception occurred 

72 self.__context__ = None 

73 

74 # The above exception was the direct cause of the following exception 

75 self.__cause__ = None 

76 

77class AccessError(UserError): 

78 """Access rights error. 

79 

80 .. admonition:: Example 

81 

82 When you try to read a record that you are not allowed to. 

83 """ 

84 http_status = 403 # Forbidden 

85 

86 

87class CacheMiss(KeyError): 

88 """Missing value(s) in cache. 

89 

90 .. admonition:: Example 

91 

92 When you try to read a value in a flushed cache. 

93 """ 

94 

95 def __init__(self, record, field): 

96 super().__init__("%r.%s" % (record, field.name)) 

97 

98 

99class MissingError(UserError): 

100 """Missing record(s). 

101 

102 .. admonition:: Example 

103 

104 When you try to write on a deleted record. 

105 """ 

106 http_status = 404 # Not Found 

107 

108 

109class LockError(UserError): 

110 """Record(s) could not be locked. 

111 

112 .. admonition:: Example 

113 

114 Code tried to lock records, but could not succeed. 

115 """ 

116 http_status = 409 # Conflict 

117 

118 

119class ValidationError(UserError): 

120 """Violation of python constraints. 

121 

122 .. admonition:: Example 

123 

124 When you try to create a new user with a login which already exist in the db. 

125 """ 

126 

127 

128class ConcurrencyError(Exception): 

129 """ 

130 Signal that two concurrent transactions tried to commit something 

131 that violates some constraint. Signal that the transaction that 

132 failed should be retried after a short delay, see 

133 :func:`~odoo.service.model.retrying`. 

134 

135 This exception is low-level and has very few use cases, it should 

136 only be used if all alternatives are deemed worse. 

137 """