def handle_request(self, request: Request) -> Response:
"""
coroutine: This method is called by Transport
implementation to handle the actual request.
It returns a webtype.Response object.
"""
# Get handler
try:
try:
handler = self.router.get_handler_for_request(request)
request.app = self
response = await handler(request)
except ResponseError as r:
response = r.response
if r.log:
exc_info = sys.exc_info()
self.logger.log_exception(request, exc_info, level='warning')
# invoke serialization (json) to make sure it works
_ = response.data
except CancelledError:
# This error can happen if a client closes the connection
# The response shouldnt really ever be used
return None
except Exception:
exc_info = sys.exc_info()
self.logger.log_exception(request, exc_info)
response = Response(status=500,
body={'message': 'Server Error'})
if not response.correlation_id:
response.correlation_id = request.correlation_id
if self._cors_handler is not None:
self._cors_handler.add_cors_headers(request, response)
# add default headers
response.headers = {**self.default_headers, **response.headers}
return response
评论列表
文章目录