diff options
| author | Tom Christie | 2012-08-26 23:06:52 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-08-26 23:06:52 +0100 |
| commit | 73cc77553ed5411f1959a51574b156a47ad5340d (patch) | |
| tree | 0b3a0f707fefd116a274adcfa80364596e8df76d /djangorestframework/exceptions.py | |
| parent | edd8f5963cb32063931a1557d3c6ac29d19b3425 (diff) | |
| download | django-rest-framework-73cc77553ed5411f1959a51574b156a47ad5340d.tar.bz2 | |
Drop ImmediateResponse
Diffstat (limited to 'djangorestframework/exceptions.py')
| -rw-r--r-- | djangorestframework/exceptions.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/djangorestframework/exceptions.py b/djangorestframework/exceptions.py index 3f7e9029..315c1b1d 100644 --- a/djangorestframework/exceptions.py +++ b/djangorestframework/exceptions.py @@ -1,9 +1,15 @@ +""" +Handled exceptions raised by REST framework. + +In addition Django's built in 403 and 404 exceptions are handled. +(`django.http.Http404` and `django.core.exceptions.PermissionDenied`) +""" from djangorestframework import status class ParseError(Exception): status_code = status.HTTP_400_BAD_REQUEST - default_detail = 'Malformed request' + default_detail = 'Malformed request.' def __init__(self, detail=None): self.detail = detail or self.default_detail @@ -11,7 +17,7 @@ class ParseError(Exception): class PermissionDenied(Exception): status_code = status.HTTP_403_FORBIDDEN - default_detail = 'You do not have permission to access this resource' + default_detail = 'You do not have permission to access this resource.' def __init__(self, detail=None): self.detail = detail or self.default_detail @@ -19,19 +25,30 @@ class PermissionDenied(Exception): class MethodNotAllowed(Exception): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = "Method '%s' not allowed" + default_detail = "Method '%s' not allowed." - def __init__(self, method, detail): + def __init__(self, method, detail=None): self.detail = (detail or self.default_detail) % method class UnsupportedMediaType(Exception): status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE - default_detail = "Unsupported media type '%s' in request" + default_detail = "Unsupported media type '%s' in request." def __init__(self, media_type, detail=None): self.detail = (detail or self.default_detail) % media_type -# class Throttled(Exception): -# def __init__(self, detail): -# self.detail = detail + +class Throttled(Exception): + status_code = status.HTTP_429_TOO_MANY_REQUESTS + default_detail = "Request was throttled. Expected available in %d seconds." + + def __init__(self, wait, detail=None): + import math + self.detail = (detail or self.default_detail) % int(math.ceil(wait)) + + +REST_FRAMEWORK_EXCEPTIONS = ( + ParseError, PermissionDenied, MethodNotAllowed, + UnsupportedMediaType, Throttled +) |
