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

1""" 

2Global Django exception and warning classes. 

3""" 

4import operator 

5 

6from django.utils.hashable import make_hashable 

7 

8 

9class FieldDoesNotExist(Exception): 

10 """The requested model field does not exist""" 

11 

12 pass 

13 

14 

15class AppRegistryNotReady(Exception): 

16 """The django.apps registry is not populated yet""" 

17 

18 pass 

19 

20 

21class ObjectDoesNotExist(Exception): 

22 """The requested object does not exist""" 

23 

24 silent_variable_failure = True 

25 

26 

27class MultipleObjectsReturned(Exception): 

28 """The query returned multiple objects when only one was expected.""" 

29 

30 pass 

31 

32 

33class SuspiciousOperation(Exception): 

34 """The user did something suspicious""" 

35 

36 

37class SuspiciousMultipartForm(SuspiciousOperation): 

38 """Suspect MIME request in multipart form data""" 

39 

40 pass 

41 

42 

43class SuspiciousFileOperation(SuspiciousOperation): 

44 """A Suspicious filesystem operation was attempted""" 

45 

46 pass 

47 

48 

49class DisallowedHost(SuspiciousOperation): 

50 """HTTP_HOST header contains invalid value""" 

51 

52 pass 

53 

54 

55class DisallowedRedirect(SuspiciousOperation): 

56 """Redirect to scheme not in allowed list""" 

57 

58 pass 

59 

60 

61class TooManyFieldsSent(SuspiciousOperation): 

62 """ 

63 The number of fields in a GET or POST request exceeded 

64 settings.DATA_UPLOAD_MAX_NUMBER_FIELDS. 

65 """ 

66 

67 pass 

68 

69 

70class RequestDataTooBig(SuspiciousOperation): 

71 """ 

72 The size of the request (excluding any file uploads) exceeded 

73 settings.DATA_UPLOAD_MAX_MEMORY_SIZE. 

74 """ 

75 

76 pass 

77 

78 

79class RequestAborted(Exception): 

80 """The request was closed before it was completed, or timed out.""" 

81 

82 pass 

83 

84 

85class BadRequest(Exception): 

86 """The request is malformed and cannot be processed.""" 

87 

88 pass 

89 

90 

91class PermissionDenied(Exception): 

92 """The user did not have permission to do that""" 

93 

94 pass 

95 

96 

97class ViewDoesNotExist(Exception): 

98 """The requested view does not exist""" 

99 

100 pass 

101 

102 

103class MiddlewareNotUsed(Exception): 

104 """This middleware is not used in this server configuration""" 

105 

106 pass 

107 

108 

109class ImproperlyConfigured(Exception): 

110 """Django is somehow improperly configured""" 

111 

112 pass 

113 

114 

115class FieldError(Exception): 

116 """Some kind of problem with a model field.""" 

117 

118 pass 

119 

120 

121NON_FIELD_ERRORS = "__all__" 

122 

123 

124class ValidationError(Exception): 

125 """An error while validating data.""" 

126 

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) 

137 

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 

145 

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 

152 

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) 

163 

164 else: 

165 self.message = message 

166 self.code = code 

167 self.params = params 

168 self.error_list = [self] 

169 

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") 

175 

176 return dict(self) 

177 

178 @property 

179 def messages(self): 

180 if hasattr(self, "error_dict"): 

181 return sum(dict(self).values(), []) 

182 return list(self) 

183 

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 

191 

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) 

202 

203 def __str__(self): 

204 if hasattr(self, "error_dict"): 

205 return repr(dict(self)) 

206 return repr(list(self)) 

207 

208 def __repr__(self): 

209 return "ValidationError(%s)" % self 

210 

211 def __eq__(self, other): 

212 if not isinstance(other, ValidationError): 

213 return NotImplemented 

214 return hash(self) == hash(other) 

215 

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")))) 

228 

229 

230class EmptyResultSet(Exception): 

231 """A database query predicate is impossible.""" 

232 

233 pass 

234 

235 

236class SynchronousOnlyOperation(Exception): 

237 """The user tried to call a sync-only function from an async context.""" 

238 

239 pass