From 68f6553181df0a824295291483ec09e71f693c9e Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Sun, 25 Dec 2016 10:37:23 -0800 Subject: [PATCH] Add invalid response object error handling --- sanic/exceptions.py | 4 ++++ sanic/server.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 369a87a2..0f5bc803 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -21,6 +21,10 @@ class ServerError(SanicException): status_code = 500 +class InvalidResponseObject(SanicException): + status_code = 500 + + class FileNotFound(NotFound): status_code = 404 diff --git a/sanic/server.py b/sanic/server.py index 11756005..f0e33780 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -6,7 +6,7 @@ from signal import SIGINT, SIGTERM from time import time from httptools import HttpRequestParser from httptools.parser.errors import HttpParserError -from .exceptions import ServerError +from .exceptions import ServerError, InvalidResponseObject try: import uvloop as async_loop @@ -150,9 +150,14 @@ class HttpProtocol(asyncio.Protocol): 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)) + try: + self.transport.write( + response.output( + self.request.version, keep_alive, + self.request_timeout)) + except AttributeError: + raise InvalidResponseObject( + 'Invalid response object, must be of type HTTPResponse') if not keep_alive: self.transport.close() else: