Inertia Exception Handler

Exception handling utilities for Inertia.js requests.

These utilities ensure that exceptions are handled correctly for both standard HTTP requests and Inertia.js requests, with proper error message formatting and response codes.

Available Functions

create_inertia_exception_response

Create an appropriate Inertia response for exceptions based on request type.

exception_to_http_response

Convert various exception types to HTTP responses with proper status codes.

litestar_vite.inertia.exception_handler.exception_to_http_response(request: Request[UserT, AuthT, StateT], exc: Exception) Response[Any][source]

Handler for all exceptions subclassed from HTTPException.

Inertia detection:

  • For InertiaRequest instances, uses the request’s derived flags (route component + headers).

  • For plain Request instances (e.g., before routing/when middleware didn’t run), falls back to checking the X-Inertia header.

Parameters:
  • request – The request object.

  • exc – The exception to handle.

Returns:

The response object.

litestar_vite.inertia.exception_handler.create_inertia_exception_response(request: Request[UserT, AuthT, StateT], exc: Exception) Response[Any][source]

Create the inertia exception response.

This function handles exceptions for Inertia-enabled routes, returning appropriate responses based on the exception type and status code.

Note

This function uses defensive programming techniques to handle edge cases: - Type-safe handling of exception extra attribute (may be string, list, dict, or None) - Graceful handling when InertiaPlugin is not registered - Broad exception handling for flash() calls (non-critical operation)

Parameters:
  • request – The request object.

  • exc – The exception to handle.

Returns:

The response object, either an InertiaResponse, InertiaRedirect, or InertiaBack.