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 turbosloth import SlothApp
from turbosloth.exceptions import NotFoundException
from turbosloth.types import Scope, Receive, Send
app = SlothApp()

View File

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