Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor error_500 #47

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tremolo/asgi_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
WebSocketClientClosed,
WebSocketServerClosed
)
from .handlers import error_500
from .lib.contexts import ServerContext
from .lib.http_protocol import HTTPProtocol
from .lib.websocket import WebSocket
Expand Down Expand Up @@ -85,6 +86,9 @@ async def headers_received(self):
# update the handler with the ASGI main task
self.handler = self.loop.create_task(self.main())

async def handle_error_500(self, exc):
return await error_500(request=self.request, exc=exc)

def connection_lost(self, exc):
if self.handler is not None and not self.handler.done():
self._set_app_close_timeout()
Expand Down
12 changes: 9 additions & 3 deletions tremolo/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class HTTPServer(HTTPProtocol):
__slots__ = ('_routes', '_middlewares', '_server')

def __init__(self, lock=None, **kwargs):
self._routes = kwargs['_routes']
self._middlewares = kwargs['_middlewares']
def __init__(self, _routes=None, _middlewares=None, lock=None, **kwargs):
self._routes = _routes
self._middlewares = _middlewares
self._server = {
'loop': kwargs['loop'],
'logger': kwargs['logger'],
Expand Down Expand Up @@ -355,3 +355,9 @@ async def headers_received(self):
self._routes[0][1][1],
{**self._routes[0][1][2], **options}
)

async def handle_error_500(self, exc):
# internal server error
return await self._routes[0][-1][1](request=self.request,
response=self.response,
exc=exc)
6 changes: 4 additions & 2 deletions tremolo/lib/http_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
async def headers_received(self):
return

async def handle_error_500(self, exc):
return

Check warning on line 157 in tremolo/lib/http_protocol.py

View check run for this annotation

Codecov / codecov/patch

tremolo/lib/http_protocol.py#L157

Added line #L157 was not covered by tests

def handler_timeout(self):
if (self.request is None or self.request.upgraded or
self.handler is None):
Expand Down Expand Up @@ -206,8 +209,7 @@
data = b''

try:
data = await self.options['_routes'][0][-1][1](
request=self.request, response=self.response, exc=exc)
data = await self.handle_error_500(exc)

if data is None:
data = b''
Expand Down