Pass HTTPException into a response

This commit is contained in:
2025-07-16 04:18:58 +03:00
parent 73da386003
commit 0e53731ea4
2 changed files with 6 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from turbosloth import SlothApp from turbosloth import SlothApp
from turbosloth.exceptions import NotFoundException
from turbosloth.types import Scope, Receive, Send from turbosloth.types import Scope, Receive, Send
app = SlothApp() app = SlothApp()

View File

@@ -1,5 +1,6 @@
from typing import Optional, Callable, Awaitable, Protocol from typing import Optional, Callable, Awaitable, Protocol
from .exceptions import HTTPException
from .router import Router from .router import Router
from .types import Scope, Receive, Send, MethodType, HandlerType from .types import Scope, Receive, Send, MethodType, HandlerType
@@ -18,21 +19,19 @@ class HTTPApp(ASGIApp):
try: try:
handler = self.router.match(method, path) handler = self.router.match(method, path)
except KeyError: await handler(scope, receive, send)
# 404 except HTTPException as e:
await send({ await send({
'type': 'http.response.start', 'type': 'http.response.start',
'status': 404, 'status': e.code,
'headers': [(b'content-type', b'text/plain')], 'headers': [(b'content-type', b'text/plain')],
}) })
await send({ await send({
'type': 'http.response.body', 'type': 'http.response.body',
'body': b'Not Found', 'body': str(e).encode(),
}) })
return return
await handler(scope, receive, send)
class WSApp(ASGIApp): class WSApp(ASGIApp):
async def _do_websocket(self, scope: Scope, receive: Receive, send: Send): async def _do_websocket(self, scope: Scope, receive: Receive, send: Send):