Skip to content

Problem

rn_forge.fastapi.problem

Render FastAPI and Starlette failures as RFC 9457 problem responses.

Register application exception mappings before installing the handlers so Starlette dispatches them through its typed exception middleware. The fallback handler also logs the current trace id.

Log = Callable[[str, Mapping[str, Any]], None]

The injected logging sink, called as log(event, context).

log_server_errors(logger=None)

Return a :data:Log that logs each 5xx problem at ERROR, with its traceback.

The record carries the event, status, instance and trace id in its message and the exception's traceback as exc_info. Events other than problem.server_error are ignored.

Parameters:

Name Type Description Default
logger AppLogger | None

Where to log. Defaults to AppLogger.get_logger("rn_forge.fastapi.problem").

None

Example::

AppConfig(log=log_server_errors())

register_problem_handlers(app, *, registry=None, realm=None, log=None)

Render every error app produces as application/problem+json.

Parameters:

Name Type Description Default
app FastAPI

The application. Call this once while building it; nothing is registered on import.

required
registry ProblemRegistry | None

The rows to render with. Defaults to a fresh :func:rn_forge.web.default_registry. RequestValidationError is registered on it against validation-error/422 unless it already has a row.

None
realm str | None

The realm of the WWW-Authenticate challenge a 401 carries when the exception brought none of its own. A 403 never carries one.

None
log Log | None

Called as log("problem.server_error", context) for every 5xx, with the exception under "exc". The body carries no detail, so this is the only place the cause goes.

None