Skip to content

Commit

Permalink
fix: missing aiohttp error handler
Browse files Browse the repository at this point in the history
AioHttpApp is handling errors like FlaskApp's common_error_handler
  • Loading branch information
ainquel committed Dec 19, 2018
1 parent 58e3fbb commit 8eda5e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
33 changes: 30 additions & 3 deletions connexion/apps/aiohttp_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,41 @@
from aiohttp.test_utils import TestClient, TestServer

from ..apis.aiohttp_api import AioHttpApi
from ..exceptions import ConnexionException
from ..exceptions import ConnexionException, ProblemException
from ..http_facts import HTTP_ERRORS
from ..lifecycle import ConnexionResponse
from ..problem import problem
from ..tests import AbstractClient
from ..utils import is_json_mimetype
from .abstract import AbstractApp

logger = logging.getLogger('connexion.aiohttp_app')


@web.middleware
@asyncio.coroutine
def error_middleware(request, handler):
error_response = None
try:
return (yield from handler(request))
except web.HTTPException as ex:
if ex.status_code >= 400:
error_response = problem(
title=HTTP_ERRORS[ex.status_code]["title"],
detail=HTTP_ERRORS[ex.status_code]["detail"],
status=ex.status_code
)
except ProblemException as exception:
error_response = exception.to_problem()
except Exception:
error_response = problem(
title=HTTP_ERRORS[500]["title"],
detail=HTTP_ERRORS[500]["detail"],
status=500
)
if error_response:
return (yield from AioHttpApi.get_response(error_response))


class AioHttpApp(AbstractApp):

api_cls = AioHttpApi
Expand All @@ -27,7 +53,7 @@ def __init__(self, import_name, only_one_api=False, **kwargs):
self._api_added = False

def create_app(self):
return web.Application(debug=self.debug)
return web.Application(debug=self.debug, middlewares=[error_middleware])

def get_root_path(self):
mod = sys.modules.get(self.import_name)
Expand All @@ -46,6 +72,7 @@ def get_root_path(self):
return os.path.dirname(os.path.abspath(filepath))

def set_errors_handlers(self):
"""error handlers are set in `create_app`."""
pass

def add_api(self, specification, **kwargs):
Expand Down
26 changes: 26 additions & 0 deletions connexion/http_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,29 @@
'application/x-www-form-urlencoded',
'multipart/form-data'
]

HTTP_ERRORS = {
404: {
"title": "Not Found",
"detail": 'The requested URL was not found on the server. '
'If you entered the URL manually please check your spelling and try again.'
},
405: {
"title": "Method Not Allowed",
"detail": "The method is not allowed for the requested URL."
},
500: {
"title": "Internal Server Error",
"detail": 'The server encountered an internal error and was unable to complete your request. '
'Either the server is overloaded or there is an error in the application.'
},
400: {
"title": "Bad Request",
"detail": "The browser (or proxy) sent a request that this server could not understand."
},
403: {
"title": "Forbidden",
"detail": "You don't have the permission to access the requested resource. "
"It is either read-protected or not readable by the server."
}
}

0 comments on commit 8eda5e3

Please sign in to comment.