Module

server.errors

Error handling pipeline for chirp requests.

Maps HTTPError exceptions and unexpected failures to appropriate Response objects, using registered error handlers or sensible defaults.

Functions

default_fragment_error 2 str
Minimal HTML snippet for fragment error responses.
def default_fragment_error(status: int, detail: str) -> str
Parameters
Name Type Description
status int
detail str
Returns
str
_with_htmx_error_headers 2 Response
Add htmx error-handling headers when the request is a fragment. Headers added:…
def _with_htmx_error_headers(response: Response, request: Request) -> Response

Add htmx error-handling headers when the request is a fragment.

Headers added:

  • HX-Retarget: #chirp-error— redirect error content to a dedicated container
  • HX-Reswap: innerHTML— replace (not append) the error content
  • HX-Trigger: chirpError— fire a client-side event for custom handling
Parameters
Name Type Description
response Response
request Request
Returns
Response
call_error_handler 4 Response
Invoke a user-registered error handler with introspected arguments. Error hand…
async
async def call_error_handler(handler: Callable[..., Any], request: Request, exc: Exception, kida_env: Environment | None) -> Response

Invoke a user-registered error handler with introspected arguments.

Error handlers may accept zero, one (request), or two (request, exc) args. Supports both sync and async error handlers.

Parameters
Name Type Description
handler Callable[..., Any]
request Request
exc Exception
kida_env Environment | None
Returns
Response
handle_http_error 5 Response
Map an HTTPError to a Response using registered error handlers.
async
async def handle_http_error(exc: HTTPError, request: Request, error_handlers: dict[int | type, Callable[..., Any]], kida_env: Environment | None, debug: bool) -> Response
Parameters
Name Type Description
exc HTTPError
request Request
error_handlers dict[int | type, Callable[..., Any]]
kida_env Environment | None
debug bool
Returns
Response
handle_internal_error 5 Response
Handle unexpected exceptions as 500 errors.
async
async def handle_internal_error(exc: Exception, request: Request, error_handlers: dict[int | type, Callable[..., Any]], kida_env: Environment | None, debug: bool) -> Response
Parameters
Name Type Description
exc Exception
request Request
error_handlers dict[int | type, Callable[..., Any]]
kida_env Environment | None
debug bool
Returns
Response