Skip to content

Commit

Permalink
Improve ErrorModel documentation
Browse files Browse the repository at this point in the history
Add safir.models to the documentation generation and add an example
of how to use the ErrorModel.
  • Loading branch information
rra committed Aug 9, 2021
1 parent 2230156 commit 8176ff7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ API reference
.. automodapi:: safir.metadata
:include-all-objects:

.. automodapi:: safir.models
:include-all-objects:

.. automodapi:: safir.middleware.x_forwarded
:include-all-objects:
28 changes: 27 additions & 1 deletion src/safir/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
"""Standard models for FastAPI applications.
Examples
--------
To reference the `ErrorModel` model when returning an error message, use code
similar to this:
.. code-block:: python
@router.get(
"/route/{foo}",
...,
responses={404: {"description": "Not found", "model": ErrorModel}},
)
async def route(foo: str) -> None:
...
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=[
{"loc": ["path", "foo"], "msg": msg, "type": "invalid_foo"},
],
)
Notes
-----
FastAPI does not appear to export its error response model in a usable form,
so define a copy of it so that we can reference it in API definitions to
generate good documentation.
"""

# Examples and notes kept in the module docstring because they're not
# appropriate for the API documentation generated for a service.

from typing import List, Optional

from pydantic import BaseModel, Field

__all__ = ["ErrorModel"]
__all__ = ["ErrorDetail", "ErrorModel"]


class ErrorDetail(BaseModel):
Expand Down

0 comments on commit 8176ff7

Please sign in to comment.