|
33 | 33 | from django.http import HttpResponse
|
34 | 34 | from rest_framework import status as HttpStatus
|
35 | 35 | from rest_framework.authentication import SessionAuthentication, BasicAuthentication
|
36 |
| -from rest_framework.exceptions import NotAuthenticated, AuthenticationFailed, MethodNotAllowed |
| 36 | +from rest_framework.exceptions import NotAuthenticated, AuthenticationFailed, MethodNotAllowed, UnsupportedMediaType, \ |
| 37 | + ParseError |
37 | 38 | from rest_framework.renderers import JSONRenderer
|
38 | 39 | from rest_framework.response import Response
|
39 | 40 | from rest_framework.settings import api_settings
|
40 |
| -from rest_framework.status import is_client_error, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED |
| 41 | +from rest_framework.status import is_client_error, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, \ |
| 42 | + HTTP_405_METHOD_NOT_ALLOWED, HTTP_403_FORBIDDEN |
41 | 43 | from rest_framework.views import APIView
|
42 |
| - |
| 44 | +from django.utils.translation import ugettext_lazy as _ |
43 | 45 | from .dto import BaseDto, ApiResponseDto
|
44 | 46 | from .error import InvalidPaginationOptionsError, InvalidInputDataError, ErrorCode
|
45 | 47 |
|
@@ -126,6 +128,23 @@ def internal_server_error(cls, exception: Exception = None):
|
126 | 128 | response.data.service.error_code = HttpStatus.HTTP_500_INTERNAL_SERVER_ERROR
|
127 | 129 | return response
|
128 | 130 |
|
| 131 | + @classmethod |
| 132 | + def not_allowed(cls): |
| 133 | + return cls.client_error( |
| 134 | + HTTP_405_METHOD_NOT_ALLOWED, |
| 135 | + "HTTP Method Not allowed.", |
| 136 | + HTTP_405_METHOD_NOT_ALLOWED, |
| 137 | + None |
| 138 | + ) |
| 139 | + |
| 140 | + @classmethod |
| 141 | + def forbidden(cls): |
| 142 | + return cls.client_error( |
| 143 | + HTTP_403_FORBIDDEN, |
| 144 | + "Forbidden", |
| 145 | + HTTP_403_FORBIDDEN |
| 146 | + ) |
| 147 | + |
129 | 148 |
|
130 | 149 | class JsonEncoder(JSONEncoder):
|
131 | 150 | def default(self, o):
|
@@ -206,13 +225,18 @@ def exception_handler(exc: Exception, context):
|
206 | 225 | if isinstance(exc, (NotAuthenticated, AuthenticationFailed)):
|
207 | 226 | return ApiResponse.not_authenticated()
|
208 | 227 | if isinstance(exc, ObjectDoesNotExist):
|
209 |
| - return ApiResponse.not_found(exc) |
| 228 | + return ApiResponse.not_found(str(exc)) |
210 | 229 | if isinstance(exc, InvalidInputDataError):
|
211 | 230 | return ApiResponse.bad_request(str(exc))
|
212 |
| - if isinstance(exc, MethodNotAllowed): |
213 |
| - return ApiResponse.client_error(HttpStatus.HTTP_405_METHOD_NOT_ALLOWED, ErrorCode(None, 'Method Not Allowed')) |
214 |
| - logger.exception(str(exc)) |
215 |
| - return ApiResponse.internal_server_error(exc) |
| 231 | + elif isinstance(exc, MethodNotAllowed): |
| 232 | + return ApiResponse.not_allowed() |
| 233 | + elif isinstance(exc, UnsupportedMediaType): |
| 234 | + return ApiResponse.bad_request(str(exc)) |
| 235 | + elif isinstance(exc, ParseError): |
| 236 | + return ApiResponse.bad_request(_("Json is invalid.")) |
| 237 | + else: |
| 238 | + logger.exception(str(exc)) |
| 239 | + return ApiResponse.internal_server_error(exc) |
216 | 240 |
|
217 | 241 |
|
218 | 242 | def __set_response_attributes(response: HttpResponse):
|
|
0 commit comments