Skip to content

Commit 74c34d5

Browse files
author
artem
committed
extend exception handler
1 parent 642b25c commit 74c34d5

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

api_commons/common.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
from django.http import HttpResponse
3434
from rest_framework import status as HttpStatus
3535
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
3738
from rest_framework.renderers import JSONRenderer
3839
from rest_framework.response import Response
3940
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
4143
from rest_framework.views import APIView
42-
44+
from django.utils.translation import ugettext_lazy as _
4345
from .dto import BaseDto, ApiResponseDto
4446
from .error import InvalidPaginationOptionsError, InvalidInputDataError, ErrorCode
4547

@@ -126,6 +128,23 @@ def internal_server_error(cls, exception: Exception = None):
126128
response.data.service.error_code = HttpStatus.HTTP_500_INTERNAL_SERVER_ERROR
127129
return response
128130

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+
129148

130149
class JsonEncoder(JSONEncoder):
131150
def default(self, o):
@@ -206,13 +225,18 @@ def exception_handler(exc: Exception, context):
206225
if isinstance(exc, (NotAuthenticated, AuthenticationFailed)):
207226
return ApiResponse.not_authenticated()
208227
if isinstance(exc, ObjectDoesNotExist):
209-
return ApiResponse.not_found(exc)
228+
return ApiResponse.not_found(str(exc))
210229
if isinstance(exc, InvalidInputDataError):
211230
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)
216240

217241

218242
def __set_response_attributes(response: HttpResponse):

0 commit comments

Comments
 (0)