Pass HTTPException into a response
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user