Skip to content

ontocast.api.responses

JSON error bodies for HTTP routes (keeps FastAPI handlers thin).

document_conversion_error_response(error, stage)

422 when an uploaded document could not be converted.

Both /process and /process_unit answer this way; previously only /process_unit did, so the same unreadable file produced 422 on one route and 500 on the other.

Source code in ontocast/api/responses.py
def document_conversion_error_response(
    error: Exception, stage: str | None
) -> JSONResponse:
    """422 when an uploaded document could not be converted.

    Both /process and /process_unit answer this way; previously only
    /process_unit did, so the same unreadable file produced 422 on one route
    and 500 on the other.
    """
    return JSONResponse(
        status_code=422,
        content=StatusErrorBody(
            error=str(error),
            error_type="DocumentConversionError",
            error_code=f"conversion_failed:{stage}" if stage else "conversion_failed",
        ).model_dump(),
    )

missing_fixed_catalog_ontology_id_response()

400 when ontology_context_mode is fixed_single_ontology but id is absent.

Source code in ontocast/api/responses.py
def missing_fixed_catalog_ontology_id_response() -> JSONResponse:
    """400 when ontology_context_mode is fixed_single_ontology but id is absent."""
    return JSONResponse(
        status_code=400,
        content=StatusErrorBody(
            error=(
                "ontology_context_mode=fixed_single_ontology requires "
                "non-empty ontology_context_fixed_ontology_id (query, form field, or JSON)."
            ),
            error_type="ValidationError",
        ).model_dump(),
    )

request_param_error_response(error)

400 for any malformed request parameter.

Replaces the previous per-message special cases, under which every parameter error but one returned 500.

Source code in ontocast/api/responses.py
def request_param_error_response(error: RequestParamError) -> JSONResponse:
    """400 for any malformed request parameter.

    Replaces the previous per-message special cases, under which every
    parameter error but one returned 500.
    """
    return JSONResponse(
        status_code=400,
        content=StatusErrorBody(
            error=str(error),
            error_type="ValidationError",
            error_code=f"invalid_param:{error.param}",
        ).model_dump(),
    )

section_selection_empty_response(error)

422 when a section selection matched nothing in this document.

Not 400: the parameters are individually well-formed and only fail against this particular document once it has been classified — the same unprocessable-entity reading a conversion failure gets.

Source code in ontocast/api/responses.py
def section_selection_empty_response(
    error: SectionSelectionEmptyError,
) -> JSONResponse:
    """422 when a section selection matched nothing in this document.

    Not 400: the parameters are individually well-formed and only fail against
    this particular document once it has been classified — the same
    unprocessable-entity reading a conversion failure gets.
    """
    return JSONResponse(
        status_code=422,
        content=StatusErrorBody(
            error=str(error),
            error_type="SectionSelectionEmpty",
            error_code=f"empty_section_selection:{error.param}",
        ).model_dump(),
    )