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