OpenCloning / OpenCloning/OpenCloning_backend

Include exceptions in API documentation

Open
#36 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
8
Forks
11
Avg merge
14m
Merged PRs (30d)
5

Description

They have to be declared here:

@app.get("/",responses={418: {"detail": "I'm a teapot"}, 413: {"Too large": "...The Payload"}})
def read_root(gimme_coffee: bool = False):
    if gimme_coffee:
        raise HTTPException(status_code=418, detail="I'm a teapot.")
    return {"Hello": "World"}

A hacky way from https://github.com/tiangolo/fastapi/issues/1999#issuecomment-735970673

A slight variation could work, if parameters were accepted, with string formatting.

from fastapi import status, HTTPException

class APIException(HTTPException):
    """Light wrapper around HTTPException that allows specifying defaults via class property"""

    status_code = status.HTTP_400_BAD_REQUEST
    detail = None
    headers = None

    def __init__(self, *args, **kwargs):
        if "status_code" not in kwargs:
            kwargs["status_code"] = self.status_code
        if "detail" not in kwargs:
            kwargs["detail"] = self.detail
        if "headers" not in kwargs:
            kwargs["headers"] = self.headers
        super().__init__(*args, **kwargs)

class TeapotException(APIException):
    status_code = 418
    detail = "I'm a teapot."

@app.get("/")
def read_root(gimme_coffee: bool = False, response_exceptions=[TeapotException]):
    if gimme_coffee:
        raise TeapotException()
    return {"Hello": "World"}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the FastAPI route declaration using responses and the HTTPException examples in the issue. Determine how declared or class-based exceptions could be represented in the generated API documentation. Done means endpoint exceptions appear in the API documentation without relying on the proposed hack.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend, documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.