-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
140 lines (97 loc) · 4.03 KB
/
exceptions.py
File metadata and controls
140 lines (97 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
# Part of modoo. See LICENSE file for full copyright and licensing details.
"""The modoo Exceptions module defines a few core exception types.
Those types are understood by the RPC layer.
Any other exception type bubbling until the RPC layer will be
treated as a 'Server error'.
.. note::
If you consider introducing new exceptions,
check out the :mod:`modoo.addons.test_exceptions` module.
"""
import logging
import warnings
_logger = logging.getLogger(__name__)
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. Semantically comparable to the generic 400 HTTP status codes.
"""
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]
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.
:param dict additional_context: parameter passed to action_id.
Can be used to limit a view to active_ids for example.
"""
def __init__(self, message, action, button_text, additional_context=None):
super().__init__(message, action, button_text, additional_context)
# 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(UserError):
"""Login/password error.
.. note::
No traceback.
.. admonition:: Example
When you try to log with a wrong password.
"""
def __init__(self, message="Access Denied"):
super().__init__(message)
self.with_traceback(None)
self.__cause__ = None
self.traceback = ('', '', '')
class AccessError(UserError):
"""Access rights error.
.. admonition:: Example
When you try to read a record that you are not allowed to.
"""
class CacheMiss(KeyError):
"""Missing value(s) in cache.
.. admonition:: Example
When you try to read a value in a flushed cache.
"""
def __init__(self, record, field):
super().__init__("%r.%s" % (record, field.name))
class MissingError(UserError):
"""Missing record(s).
.. admonition:: Example
When you try to write on a deleted record.
"""
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.
"""
# Deprecated exceptions, only kept for backward compatibility, may be
# removed in the future *without* any further notice than the Deprecation
# Warning.
class except_orm(UserError):
def __init__(self, name, value=None):
warnings.warn("except_orm is a deprecated alias to UserError.", DeprecationWarning)
super().__init__(f"{name}: {value}")
class Warning(UserError):
def __init__(self, *args, **kwargs):
warnings.warn("Warning is a deprecated alias to UserError.", DeprecationWarning)
super().__init__(*args, **kwargs)
class QWebException(Exception):
def __init__(self, *args, **kwargs):
warnings.warn("qweb.QWebException is the exception you are looking for.", DeprecationWarning)
super().__init__(*args, **kwargs)