Skip to content

Commit

Permalink
Return JSONResponse in ExceptionMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Apr 25, 2022
1 parent 90fba11 commit c1004c7
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions connexion/middleware/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json

from starlette.exceptions import \
ExceptionMiddleware as StarletteExceptionMiddleware
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.responses import Response
from starlette.responses import JSONResponse

from connexion.exceptions import ProblemException, problem

Expand All @@ -17,7 +15,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_exception_handler(ProblemException, self.problem_handler)

def problem_handler(self, _, exception: ProblemException):
def problem_handler(self, _, exception: ProblemException) -> JSONResponse:
"""
:type exception: Exception
"""
Expand All @@ -31,14 +29,14 @@ def problem_handler(self, _, exception: ProblemException):
ext=exception.ext
)

return Response(
content=json.dumps(connexion_response.body),
return JSONResponse(
content=connexion_response.body,
status_code=connexion_response.status_code,
media_type=connexion_response.mimetype,
headers=connexion_response.headers
)

def http_exception(self, request: Request, exc: HTTPException) -> Response:
def http_exception(self, request: Request, exc: HTTPException) -> JSONResponse:
try:
headers = exc.headers
except AttributeError:
Expand All @@ -50,8 +48,8 @@ def http_exception(self, request: Request, exc: HTTPException) -> Response:
status=exc.status_code,
headers=headers)

return Response(
content=json.dumps(connexion_response.body),
return JSONResponse(
content=connexion_response.body,
status_code=connexion_response.status_code,
media_type=connexion_response.mimetype,
headers=connexion_response.headers
Expand Down

0 comments on commit c1004c7

Please sign in to comment.