Skip to content

Instantly share code, notes, and snippets.

@hatarist
Last active September 8, 2017 15:36
Show Gist options
  • Save hatarist/7644fccf15f3179c6ae94e76c9eb02e5 to your computer and use it in GitHub Desktop.
Save hatarist/7644fccf15f3179c6ae94e76c9eb02e5 to your computer and use it in GitHub Desktop.
A quick Sanic workaround that doesn't complain about weird HTTP 408 errors that lead to the blank access.log entries
from sanic.exceptions import RequestTimeout
from sanic.server import HttpProtocol
class NoTimeoutHttpProtocol(HttpProtocol):
"""
Doesn't complain about request timeouts to the logger.
Usage:
app = Sanic()
app.run(..., protocol=NoTimeoutHttpProtocol)
"""
def write_error(self, exception):
if isinstance(exception, RequestTimeout):
response = self.error_handler.response(self.request, exception)
version = self.request.version if self.request else '1.1'
self.transport.write(response.output(version))
self.transport.close()
else:
return super().write_error(exception)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment