Exceptions API#

fastapi_restly.exc defines the public exception hierarchy: configuration errors raised when the framework is misused at setup, and request-time HTTP errors that subclass fastapi.HTTPException.

Public exception hierarchy for FastAPI-Restly.

Two families:

  • Configuration-time errors (RestlyError / RestlyConfigurationError) raised when the framework is misused before/at setup.

  • Request-time HTTP errors (NotFound / Forbidden / Conflict / BadQueryParam) raised while handling a request. These subclass fastapi.HTTPException, so the default responses are identical to raising HTTPException directly – but a user can app.add_exception_handler(fr.exc.NotFound, ...) to reshape Restly’s errors distinctly (e.g. into RFC 9457 problem+json).

exception fastapi_restly.exc.BadQueryParam(detail: object | None = None, **kwargs: object)#

Bases: RestlyHTTPError

HTTP 400 – a value that the list params did not validate, and that Restly cannot apply.

An example is a key inside the filter of a react-admin view, or a value given to FilterKey.parse. On a list route, a query string that does not match the list params, such as an unknown key or a filter value of the wrong type, is a 422 instead: a RequestValidationError, in the shape that FastAPI uses for a bad parameter.

status_code: int = 400#

The HTTP status of the response. Each error class sets its own.

default_detail: str = 'Invalid query parameter'#

The detail of the response when the error is raised without a detail. Each error class sets its own.

exception fastapi_restly.exc.Conflict(detail: object | None = None, **kwargs: object)#

Bases: RestlyHTTPError

HTTP 409 – the request conflicts with the current resource state.

Restly raises it for a database conflict, such as a duplicate unique value, in a CRUD write or a write_action block. The SQLAlchemy IntegrityError is its __cause__.

status_code: int = 409#

The HTTP status of the response. Each error class sets its own.

default_detail: str = 'Conflict'#

The detail of the response when the error is raised without a detail. Each error class sets its own.

exception fastapi_restly.exc.Forbidden(detail: object | None = None, **kwargs: object)#

Bases: RestlyHTTPError

HTTP 403 – the request is not authorized.

status_code: int = 403#

The HTTP status of the response. Each error class sets its own.

default_detail: str = 'Forbidden'#

The detail of the response when the error is raised without a detail. Each error class sets its own.

exception fastapi_restly.exc.NotFound(detail: object | None = None, **kwargs: object)#

Bases: RestlyHTTPError

HTTP 404 – the requested resource does not exist (or is not visible).

status_code: int = 404#

The HTTP status of the response. Each error class sets its own.

default_detail: str = 'Not found'#

The detail of the response when the error is raised without a detail. Each error class sets its own.

exception fastapi_restly.exc.RestlyConfigurationError#

Bases: RestlyError

Raised when a setup cannot work: a view, a model, a schema or the test setup that is declared wrongly, or Restly used before it is configured.

exception fastapi_restly.exc.RestlyDuplicateSchemaNameWarning#

Bases: UserWarning

Emitted when more than one class in the OpenAPI spec has the same name.

OpenAPI keeps all classes in one list, by name. So pydantic shows the classes under long names that can change, such as app__tasks__views__TaskCreate, and generated clients use these names as type names. The warning names the classes and the CRUD views that use them.

Restly checks the spec each time the app builds it. It knows the app when you pass it to configure() or configure_tests(), or when you include a view on the app itself. Then the check also covers the views that you include on an APIRouter. There is no setting that turns the check off. A project test can turn the warning into an error with warnings.simplefilter("error", RestlyDuplicateSchemaNameWarning).

exception fastapi_restly.exc.RestlyError#

Bases: Exception

Base class of Restly’s configuration errors.

The request-time HTTP errors, such as NotFound, do not derive from it. They subclass RestlyHTTPError, a fastapi.HTTPException.

exception fastapi_restly.exc.RestlyExpireOnCommitWarning#

Bases: UserWarning

Emitted when a view writes through a session with expire_on_commit=True, once for each view.

The commit expires the loaded attributes of the saved row. So snapshot(new) in after_action_commit is empty, and the response loads the row from the database again. In async code, that raises MissingGreenlet. Restly’s own session factories set expire_on_commit=False. A sync project that keeps expire_on_commit=True on purpose can filter this warning by its class.

exception fastapi_restly.exc.RestlyHTTPError(detail: object | None = None, **kwargs: object)#

Bases: HTTPException

Base for Restly’s request-time HTTP errors. Subclasses set a status.

status_code: int = 500#

The HTTP status of the response. Each error class sets its own.

default_detail: str = 'Error'#

The detail of the response when the error is raised without a detail. Each error class sets its own.

exception fastapi_restly.exc.RestlyMisuseWarning#

Bases: UserWarning

Emitted at view registration for common framework-misuse patterns.

Opt-in: enable with fr.configure(warn_on_misuse=True). When a view class is registered via include_view, the framework then flags the dominant misuses – overriding an endpoint method (<verb>_endpoint) where a business-method override was meant, calling session.commit() directly in a method of a CRUD view, typing a scalar foreign-key column (post_id) as an IDRef / IDSchema reference instead of fr.MustExist[int, Model], and a writable field that sets a column that the model sets (init=False with a default) instead of fr.ReadOnly. It also flags a view that is registered twice on the same app or router: the second include_view call does nothing.

exception fastapi_restly.exc.RestlySkippedFilterMethodsWarning#

Bases: UserWarning

Emitted when a view filters a list with list params that do not run its filter methods.

A view runs the methods from list_filter() only for its own list params: those of a list route, or a dict or a QueryParams that a method gets. A Pydantic model of another class, such as one of your own, does not run them. Then the list can have rows that a method would leave out. Pass the view’s list_params_schema, a dict or a QueryParams instead. To skip the methods on purpose, turn the warning off with warnings.filterwarnings.

exception fastapi_restly.exc.RestlyUncommittedChangesWarning#

Bases: UserWarning

Emitted when a request finishes with uncommitted changes in the session.

The write handlers own the commit, so a custom write route that flushes (e.g. with session.flush()) but never commits has its changes silently rolled back when the session closes. The fix is almost always to commit: bracket the mutation with write_action(...) (the framework then runs the commit), or reuse a handle_<verb> handler. A route that intentionally rolls back (a validate-then-rollback dry run) can suppress the warning for just that request with session.info["_fr_suppress_uncommitted"] = True.

See also

Errors explains which exception to raise where and how to change the error envelope app-wide.