App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / core / exceptions.py
blob201a854a4bea321a5af277901b929fd0f2e62124
1 """
2 Global Django exception and warning classes.
3 """
5 class DjangoRuntimeWarning(RuntimeWarning):
6 pass
8 class ObjectDoesNotExist(Exception):
9 "The requested object does not exist"
10 silent_variable_failure = True
12 class MultipleObjectsReturned(Exception):
13 "The query returned multiple objects when only one was expected."
14 pass
16 class SuspiciousOperation(Exception):
17 "The user did something suspicious"
18 pass
20 class PermissionDenied(Exception):
21 "The user did not have permission to do that"
22 pass
24 class ViewDoesNotExist(Exception):
25 "The requested view does not exist"
26 pass
28 class MiddlewareNotUsed(Exception):
29 "This middleware is not used in this server configuration"
30 pass
32 class ImproperlyConfigured(Exception):
33 "Django is somehow improperly configured"
34 pass
36 class FieldError(Exception):
37 """Some kind of problem with a model field."""
38 pass
40 NON_FIELD_ERRORS = '__all__'
41 class ValidationError(Exception):
42 """An error while validating data."""
43 def __init__(self, message, code=None, params=None):
44 import operator
45 from django.utils.encoding import force_unicode
46 """
47 ValidationError can be passed any object that can be printed (usually
48 a string), a list of objects or a dictionary.
49 """
50 if isinstance(message, dict):
51 self.message_dict = message
52 # Reduce each list of messages into a single list.
53 message = reduce(operator.add, message.values())
55 if isinstance(message, list):
56 self.messages = [force_unicode(msg) for msg in message]
57 else:
58 self.code = code
59 self.params = params
60 message = force_unicode(message)
61 self.messages = [message]
63 def __str__(self):
64 # This is needed because, without a __str__(), printing an exception
65 # instance would result in this:
66 # AttributeError: ValidationError instance has no attribute 'args'
67 # See http://www.python.org/doc/current/tut/node10.html#handling
68 if hasattr(self, 'message_dict'):
69 return repr(self.message_dict)
70 return repr(self.messages)
72 def __repr__(self):
73 if hasattr(self, 'message_dict'):
74 return 'ValidationError(%s)' % repr(self.message_dict)
75 return 'ValidationError(%s)' % repr(self.messages)
77 def update_error_dict(self, error_dict):
78 if hasattr(self, 'message_dict'):
79 if error_dict:
80 for k, v in self.message_dict.items():
81 error_dict.setdefault(k, []).extend(v)
82 else:
83 error_dict = self.message_dict
84 else:
85 error_dict[NON_FIELD_ERRORS] = self.messages
86 return error_dict