From d91284f234ed27327e5557620d4bc5af14337bae Mon Sep 17 00:00:00 2001 From: hideyukiMORI Date: Tue, 19 May 2026 22:26:17 +0900 Subject: [PATCH] =?UTF-8?q?docs:=20DomainExceptionHandlerProtocol=20?= =?UTF-8?q?=E3=81=AE=E5=AE=9F=E8=A3=85=E4=BE=8B=E3=82=92=E3=83=AA=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=83=AC=E3=83=B3=E3=82=B9=E3=81=AB=E8=BF=BD=E8=A8=98?= =?UTF-8?q?=20(#64)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handles()/handle() シグネチャと ErrorHandlerMiddleware への登録方法を コード例付きで framework-modules.md に追加。 Co-Authored-By: Claude Sonnet 4.6 --- docs/reference/framework-modules.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/reference/framework-modules.md b/docs/reference/framework-modules.md index 1380b4a..b84c9e3 100644 --- a/docs/reference/framework-modules.md +++ b/docs/reference/framework-modules.md @@ -97,6 +97,35 @@ See [Configuration reference](configuration.md) for all fields. Catches all unhandled exceptions and converts them to Problem Details responses. Register domain exception handlers via `DomainExceptionHandlerProtocol`. +```python +from starlette.responses import Response +from nene2.http import problem_details_response +from nene2.middleware import ErrorHandlerMiddleware +from nene2.middleware.domain_exception import DomainExceptionHandlerProtocol + +class NoteNotFoundExceptionHandler: + def handles(self, exc: Exception) -> bool: + return isinstance(exc, NoteNotFoundException) + + def handle(self, exc: Exception) -> Response: + assert isinstance(exc, NoteNotFoundException) + return problem_details_response("not-found", "Not Found", 404, str(exc)) + +# Registration — pass as domain_handlers list: +app.add_middleware( + ErrorHandlerMiddleware, + debug=settings.app_debug, + domain_handlers=[NoteNotFoundExceptionHandler()], +) +``` + +`DomainExceptionHandlerProtocol` requires two methods: + +| Method | Signature | Purpose | +|---|---|---| +| `handles` | `(exc: Exception) -> bool` | Return `True` if this handler owns the exception | +| `handle` | `(exc: Exception) -> Response` | Convert exception to an HTTP response | + ### Other middleware | Class | Module | Role |