diff options
| author | Craig Blaszczyk | 2015-01-07 11:41:06 +0000 |
|---|---|---|
| committer | Craig Blaszczyk | 2015-01-07 11:41:06 +0000 |
| commit | 9b4177b6ea38de6e86b0fe723834b6ef36af15b3 (patch) | |
| tree | 3de7ee4886dc396c128143889bce4d7c37fbf283 /rest_framework/exceptions.py | |
| parent | 7913947757b0e6bd1b8828db81933c32c498e20a (diff) | |
| download | django-rest-framework-9b4177b6ea38de6e86b0fe723834b6ef36af15b3.tar.bz2 | |
switch to using format strings in error messages; raise NotFound when pagination fails to provide a more useful error message
Diffstat (limited to 'rest_framework/exceptions.py')
| -rw-r--r-- | rest_framework/exceptions.py | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index c8cedfce..dfc57293 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -7,8 +7,7 @@ In addition Django's built in 403 and 404 exceptions are handled. from __future__ import unicode_literals from django.utils import six from django.utils.encoding import force_text -from django.utils.translation import ugettext_lazy as _ -from django.utils.translation import ungettext_lazy +from django.utils.translation import ugettext_lazy as _, ungettext from rest_framework import status import math @@ -96,13 +95,13 @@ class NotFound(APIException): class MethodNotAllowed(APIException): status_code = status.HTTP_405_METHOD_NOT_ALLOWED - default_detail = _("Method '%s' not allowed.") + default_detail = _("Method {method} not allowed.") def __init__(self, method, detail=None): if detail is not None: self.detail = force_text(detail) else: - self.detail = force_text(self.default_detail) % method + self.detail = force_text(self.default_detail).format(method=method) class NotAcceptable(APIException): @@ -119,23 +118,22 @@ class NotAcceptable(APIException): class UnsupportedMediaType(APIException): status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE - default_detail = _("Unsupported media type '%s' in request.") + default_detail = _("Unsupported media type '{media_type}' in request.") def __init__(self, media_type, detail=None): if detail is not None: self.detail = force_text(detail) else: - self.detail = force_text(self.default_detail) % media_type + self.detail = force_text(self.default_detail).format( + media_type=media_type + ) class Throttled(APIException): status_code = status.HTTP_429_TOO_MANY_REQUESTS default_detail = _('Request was throttled.') - extra_detail = ungettext_lazy( - 'Expected available in %(wait)d second.', - 'Expected available in %(wait)d seconds.', - 'wait' - ) + extra_detail_singular = 'Expected available in {wait} second.' + extra_detail_plural = 'Expected available in {wait} seconds.' def __init__(self, wait=None, detail=None): if detail is not None: @@ -147,6 +145,8 @@ class Throttled(APIException): self.wait = None else: self.wait = math.ceil(wait) - self.detail += ' ' + force_text( - self.extra_detail % {'wait': self.wait} - ) + self.detail += ' ' + force_text(ungettext( + self.extra_detail_singular.format(wait=self.wait), + self.extra_detail_plural.format(wait=self.wait), + self.wait + )) |
