Debian Codegraph @

src/drf_yasg/middleware.py

raw symbols SCIP
1 from django.http import HttpResponse
2
3 from .codecs import _OpenAPICodec
4 from .errors import SwaggerValidationError
5
6
7 class SwaggerExceptionMiddleware:
8 def __init__(self, get_response):
9 self.get_response = get_response
10
11 def __call__(self, request):
12 return self.get_response(request)
13
14 def process_exception(self, request, exception):
15 if isinstance(exception, SwaggerValidationError):
16 err = {"errors": exception.errors, "message": str(exception)}
17 codec = exception.source_codec
18 if isinstance(codec, _OpenAPICodec):
19 err = codec.encode_error(err)
20 content_type = codec.media_type
21 return HttpResponse(err, status=500, content_type=content_type)
22
23 return None # pragma: no cover