Skip to content

Commit

Permalink
refactor error_500
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Nov 30, 2023
1 parent b785930 commit 6085748
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
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 put_to_queue(
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 @@ async def handle_exception(self, exc):
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

0 comments on commit 6085748

Please sign in to comment.