diff --git a/src/turbosloth/__main__.py b/src/turbosloth/__main__.py index 07b8021..30a4f70 100644 --- a/src/turbosloth/__main__.py +++ b/src/turbosloth/__main__.py @@ -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() diff --git a/src/turbosloth/app.py b/src/turbosloth/app.py index 7a9d9cb..a47ae75 100644 --- a/src/turbosloth/app.py +++ b/src/turbosloth/app.py @@ -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):