From 04c8f4a5edc2c534ba4f43cf2867d214b99bd6cf Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Thu, 16 Feb 2017 17:49:14 -0600 Subject: [PATCH] Adds some safety around response types being wrong Before we didn't check if the response object was actually a response object. This PR catches the exception should it happen and reports it to the user. --- sanic/server.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index 99652fc9..67aa771e 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -167,12 +167,18 @@ class HttpProtocol(asyncio.Protocol): # -------------------------------------------- # def write_response(self, response): + keep_alive = ( + self.parser.should_keep_alive() and not self.signal.stopped) try: - keep_alive = ( - self.parser.should_keep_alive() and not self.signal.stopped) self.transport.write( response.output( self.request.version, keep_alive, self.request_timeout)) + except AttributeError: + log.error( + ('Invalid response object for url {}, ' + 'Expected Type: HTTPResponse, Actual Type: {}').format( + self.url, type(response))) + self.write_error(ServerError('Invalid response type')) except RuntimeError: log.error( 'Connection lost before response written @ {}'.format(