Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/exceptions.py: 51%
105 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2Global Django exception and warning classes.
3"""
4import operator
6from django.utils.hashable import make_hashable
9class FieldDoesNotExist(Exception):
10 """The requested model field does not exist"""
12 pass
15class AppRegistryNotReady(Exception):
16 """The django.apps registry is not populated yet"""
18 pass
21class ObjectDoesNotExist(Exception):
22 """The requested object does not exist"""
24 silent_variable_failure = True
27class MultipleObjectsReturned(Exception):
28 """The query returned multiple objects when only one was expected."""
30 pass
33class SuspiciousOperation(Exception):
34 """The user did something suspicious"""
37class SuspiciousMultipartForm(SuspiciousOperation):
38 """Suspect MIME request in multipart form data"""
40 pass
43class SuspiciousFileOperation(SuspiciousOperation):
44 """A Suspicious filesystem operation was attempted"""
46 pass
49class DisallowedHost(SuspiciousOperation):
50 """HTTP_HOST header contains invalid value"""
52 pass
55class DisallowedRedirect(SuspiciousOperation):
56 """Redirect to scheme not in allowed list"""
58 pass
61class TooManyFieldsSent(SuspiciousOperation):
62 """
63 The number of fields in a GET or POST request exceeded
64 settings.DATA_UPLOAD_MAX_NUMBER_FIELDS.
65 """
67 pass
70class RequestDataTooBig(SuspiciousOperation):
71 """
72 The size of the request (excluding any file uploads) exceeded
73 settings.DATA_UPLOAD_MAX_MEMORY_SIZE.
74 """
76 pass
79class RequestAborted(Exception):
80 """The request was closed before it was completed, or timed out."""
82 pass
85class BadRequest(Exception):
86 """The request is malformed and cannot be processed."""
88 pass
91class PermissionDenied(Exception):
92 """The user did not have permission to do that"""
94 pass
97class ViewDoesNotExist(Exception):
98 """The requested view does not exist"""
100 pass
103class MiddlewareNotUsed(Exception):
104 """This middleware is not used in this server configuration"""
106 pass
109class ImproperlyConfigured(Exception):
110 """Django is somehow improperly configured"""
112 pass
115class FieldError(Exception):
116 """Some kind of problem with a model field."""
118 pass
121NON_FIELD_ERRORS = "__all__"
124class ValidationError(Exception):
125 """An error while validating data."""
127 def __init__(self, message, code=None, params=None):
128 """
129 The `message` argument can be a single error, a list of errors, or a
130 dictionary that maps field names to lists of errors. What we define as
131 an "error" can be either a simple string or an instance of
132 ValidationError with its message attribute set, and what we define as
133 list or dictionary can be an actual `list` or `dict` or an instance
134 of ValidationError with its `error_list` or `error_dict` attribute set.
135 """
136 super().__init__(message, code, params)
138 if isinstance(message, ValidationError):
139 if hasattr(message, "error_dict"):
140 message = message.error_dict
141 elif not hasattr(message, "message"):
142 message = message.error_list
143 else:
144 message, code, params = message.message, message.code, message.params
146 if isinstance(message, dict):
147 self.error_dict = {}
148 for field, messages in message.items():
149 if not isinstance(messages, ValidationError):
150 messages = ValidationError(messages)
151 self.error_dict[field] = messages.error_list
153 elif isinstance(message, list):
154 self.error_list = []
155 for message in message:
156 # Normalize plain strings to instances of ValidationError.
157 if not isinstance(message, ValidationError):
158 message = ValidationError(message)
159 if hasattr(message, "error_dict"):
160 self.error_list.extend(sum(message.error_dict.values(), []))
161 else:
162 self.error_list.extend(message.error_list)
164 else:
165 self.message = message
166 self.code = code
167 self.params = params
168 self.error_list = [self]
170 @property
171 def message_dict(self):
172 # Trigger an AttributeError if this ValidationError
173 # doesn't have an error_dict.
174 getattr(self, "error_dict")
176 return dict(self)
178 @property
179 def messages(self):
180 if hasattr(self, "error_dict"):
181 return sum(dict(self).values(), [])
182 return list(self)
184 def update_error_dict(self, error_dict):
185 if hasattr(self, "error_dict"):
186 for field, error_list in self.error_dict.items():
187 error_dict.setdefault(field, []).extend(error_list)
188 else:
189 error_dict.setdefault(NON_FIELD_ERRORS, []).extend(self.error_list)
190 return error_dict
192 def __iter__(self):
193 if hasattr(self, "error_dict"):
194 for field, errors in self.error_dict.items():
195 yield field, list(ValidationError(errors))
196 else:
197 for error in self.error_list:
198 message = error.message
199 if error.params:
200 message %= error.params
201 yield str(message)
203 def __str__(self):
204 if hasattr(self, "error_dict"):
205 return repr(dict(self))
206 return repr(list(self))
208 def __repr__(self):
209 return "ValidationError(%s)" % self
211 def __eq__(self, other):
212 if not isinstance(other, ValidationError):
213 return NotImplemented
214 return hash(self) == hash(other)
216 def __hash__(self):
217 if hasattr(self, "message"):
218 return hash(
219 (
220 self.message,
221 self.code,
222 make_hashable(self.params),
223 )
224 )
225 if hasattr(self, "error_dict"):
226 return hash(make_hashable(self.error_dict))
227 return hash(tuple(sorted(self.error_list, key=operator.attrgetter("message"))))
230class EmptyResultSet(Exception):
231 """A database query predicate is impossible."""
233 pass
236class SynchronousOnlyOperation(Exception):
237 """The user tried to call a sync-only function from an async context."""
239 pass