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