diff --git a/.coveragerc b/.coveragerc index f4c5b83f..f0624d2e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,7 +1,7 @@ [run] branch = True -source = sanic, tests -omit = site-packages +source = sanic +omit = site-packages, sanic/utils.py [html] -directory = coverage \ No newline at end of file +directory = coverage diff --git a/docs/sanic/extensions.md b/docs/sanic/extensions.md index fe0f223a..efd25d2c 100644 --- a/docs/sanic/extensions.md +++ b/docs/sanic/extensions.md @@ -11,3 +11,4 @@ A list of Sanic extensions created by the community. - [Pagination](https://github.com/lixxu/python-paginate): Simple pagination support. - [Motor](https://github.com/lixxu/sanic-motor): Simple motor wrapper. - [Sanic CRUD](https://github.com/Typhon66/sanic_crud): CRUD REST API generation with peewee models. +- [UserAgent](https://github.com/lixxu/sanic-useragent): Add `user_agent` to request diff --git a/sanic/server.py b/sanic/server.py index 99652fc9..83bda3fc 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -167,19 +167,26 @@ 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( self.request.ip)) except Exception as e: self.bail_out( - "Writing response failed, connection closed {}".format(e)) + "Writing response failed, connection closed {}".format( + repr(e))) finally: if not keep_alive: self.transport.close() @@ -196,10 +203,10 @@ class HttpProtocol(asyncio.Protocol): except RuntimeError: log.error( 'Connection lost before error written @ {}'.format( - self.request.ip)) + self.request.ip if self.request else 'Unknown')) except Exception as e: self.bail_out( - "Writing error failed, connection closed {}".format(e), + "Writing error failed, connection closed {}".format(repr(e)), from_error=True) finally: self.transport.close() diff --git a/tox.ini b/tox.ini index 4155e909..05276a43 100644 --- a/tox.ini +++ b/tox.ini @@ -14,10 +14,13 @@ deps = aiohttp pytest beautifulsoup4 + coverage commands = pytest tests {posargs} - + coverage erase + coverage run -m sanic.app + coverage report [testenv:flake8] deps =