1"""
2Global Django exception classes.
3"""
4
5import operator
6
7from django.utils.hashable import make_hashable
8
9
10class FieldDoesNotExist(Exception):
11 """The requested model field does not exist"""
12
13 pass
14
15
16class AppRegistryNotReady(Exception):
17 """The django.apps registry is not populated yet"""
18
19 pass
20
21
22class ObjectDoesNotExist(Exception):
23 """The requested object does not exist"""
24
25 silent_variable_failure = True
26
27
28class MultipleObjectsReturned(Exception):
29 """The query returned multiple objects when only one was expected."""
30
31 pass
32
33
34class SuspiciousOperation(Exception):
35 """The user did something suspicious"""
36
37
38class SuspiciousMultipartForm(SuspiciousOperation):
39 """Suspect MIME request in multipart form data"""
40
41 pass
42
43
44class SuspiciousFileOperation(SuspiciousOperation):
45 """A Suspicious filesystem operation was attempted"""
46
47 pass
48
49
50class DisallowedHost(SuspiciousOperation):
51 """HTTP_HOST header contains invalid value"""
52
53 pass
54
55
56class DisallowedRedirect(SuspiciousOperation):
57 """Redirect to scheme not in allowed list"""
58
59 pass
60
61
62class TooManyFieldsSent(SuspiciousOperation):
63 """
64 The number of fields in a GET or POST request exceeded
65 settings.DATA_UPLOAD_MAX_NUMBER_FIELDS.
66 """
67
68 pass
69
70
71class TooManyFilesSent(SuspiciousOperation):
72 """
73 The number of fields in a GET or POST request exceeded
74 settings.DATA_UPLOAD_MAX_NUMBER_FILES.
75 """
76
77 pass
78
79
80class RequestDataTooBig(SuspiciousOperation):
81 """
82 The size of the request (excluding any file uploads) exceeded
83 settings.DATA_UPLOAD_MAX_MEMORY_SIZE.
84 """
85
86 pass
87
88
89class RequestAborted(Exception):
90 """The request was closed before it was completed, or timed out."""
91
92 pass
93
94
95class BadRequest(Exception):
96 """The request is malformed and cannot be processed."""
97
98 pass
99
100
101class PermissionDenied(Exception):
102 """The user did not have permission to do that"""
103
104 pass
105
106
107class ViewDoesNotExist(Exception):
108 """The requested view does not exist"""
109
110 pass
111
112
113class MiddlewareNotUsed(Exception):
114 """This middleware is not used in this server configuration"""
115
116 pass
117
118
119class ImproperlyConfigured(Exception):
120 """Django is somehow improperly configured"""
121
122 pass
123
124
125class FieldError(Exception):
126 """Some kind of problem with a model field."""
127
128 pass
129
130
131NON_FIELD_ERRORS = "__all__"
132
133
134class ValidationError(Exception):
135 """An error while validating data."""
136
137 def __init__(self, message, code=None, params=None):
138 """
139 The `message` argument can be a single error, a list of errors, or a
140 dictionary that maps field names to lists of errors. What we define as
141 an "error" can be either a simple string or an instance of
142 ValidationError with its message attribute set, and what we define as
143 list or dictionary can be an actual `list` or `dict` or an instance
144 of ValidationError with its `error_list` or `error_dict` attribute set.
145 """
146 super().__init__(message, code, params)
147
148 if isinstance(message, ValidationError):
149 if hasattr(message, "error_dict"):
150 message = message.error_dict
151 elif not hasattr(message, "message"):
152 message = message.error_list
153 else:
154 message, code, params = message.message, message.code, message.params
155
156 if isinstance(message, dict):
157 self.error_dict = {}
158 for field, messages in message.items():
159 if not isinstance(messages, ValidationError):
160 messages = ValidationError(messages)
161 self.error_dict[field] = messages.error_list
162
163 elif isinstance(message, list):
164 self.error_list = []
165 for message in message:
166 # Normalize plain strings to instances of ValidationError.
167 if not isinstance(message, ValidationError):
168 message = ValidationError(message)
169 if hasattr(message, "error_dict"):
170 self.error_list.extend(sum(message.error_dict.values(), []))
171 else:
172 self.error_list.extend(message.error_list)
173
174 else:
175 self.message = message
176 self.code = code
177 self.params = params
178 self.error_list = [self]
179
180 @property
181 def message_dict(self):
182 # Trigger an AttributeError if this ValidationError
183 # doesn't have an error_dict.
184 getattr(self, "error_dict")
185
186 return dict(self)
187
188 @property
189 def messages(self):
190 if hasattr(self, "error_dict"):
191 return sum(dict(self).values(), [])
192 return list(self)
193
194 def update_error_dict(self, error_dict):
195 if hasattr(self, "error_dict"):
196 for field, error_list in self.error_dict.items():
197 error_dict.setdefault(field, []).extend(error_list)
198 else:
199 error_dict.setdefault(NON_FIELD_ERRORS, []).extend(self.error_list)
200 return error_dict
201
202 def __iter__(self):
203 if hasattr(self, "error_dict"):
204 for field, errors in self.error_dict.items():
205 yield field, list(ValidationError(errors))
206 else:
207 for error in self.error_list:
208 message = error.message
209 if error.params:
210 message %= error.params
211 yield str(message)
212
213 def __str__(self):
214 if hasattr(self, "error_dict"):
215 return repr(dict(self))
216 return repr(list(self))
217
218 def __repr__(self):
219 return "ValidationError(%s)" % self
220
221 def __eq__(self, other):
222 if not isinstance(other, ValidationError):
223 return NotImplemented
224 return hash(self) == hash(other)
225
226 def __hash__(self):
227 if hasattr(self, "message"):
228 return hash(
229 (
230 self.message,
231 self.code,
232 make_hashable(self.params),
233 )
234 )
235 if hasattr(self, "error_dict"):
236 return hash(make_hashable(self.error_dict))
237 return hash(tuple(sorted(self.error_list, key=operator.attrgetter("message"))))
238
239
240class EmptyResultSet(Exception):
241 """A database query predicate is impossible."""
242
243 pass
244
245
246class FullResultSet(Exception):
247 """A database query predicate is matches everything."""
248
249 pass
250
251
252class SynchronousOnlyOperation(Exception):
253 """The user tried to call a sync-only function from an async context."""
254
255 pass