Home / File/ test_exception_handlers.py — fastapi Source File

test_exception_handlers.py — fastapi Source File

Architecture documentation for test_exception_handlers.py, a python file in the fastapi codebase. 6 imports, 0 dependents.

File python FastAPI Routing 6 imports 14 functions

Entity Profile

Dependency Diagram

graph LR
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618["test_exception_handlers.py"]
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  01c652c5_d85c_f45e_848e_412c94ea4172["exceptions.py"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> 01c652c5_d85c_f45e_848e_412c94ea4172
  58bb043a_10d8_c308_5564_225558a63815["RequestValidationError"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> 58bb043a_10d8_c308_5564_225558a63815
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  e5a24867_6a9b_cc44_1a92_bc955a4ceb50["starlette.responses"]
  49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 --> e5a24867_6a9b_cc44_1a92_bc955a4ceb50
  style 49e73d8b_45e4_f5d6_4dfd_6c0ce0292618 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import pytest
from fastapi import Depends, FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.testclient import TestClient
from starlette.responses import JSONResponse


def http_exception_handler(request, exception):
    return JSONResponse({"exception": "http-exception"})


def request_validation_exception_handler(request, exception):
    return JSONResponse({"exception": "request-validation"})


def server_error_exception_handler(request, exception):
    return JSONResponse(status_code=500, content={"exception": "server-error"})


app = FastAPI(
    exception_handlers={
        HTTPException: http_exception_handler,
        RequestValidationError: request_validation_exception_handler,
        Exception: server_error_exception_handler,
    }
)

client = TestClient(app)


def raise_value_error():
    raise ValueError()


def dependency_with_yield():
    yield raise_value_error()


@app.get("/dependency-with-yield", dependencies=[Depends(dependency_with_yield)])
def with_yield(): ...


@app.get("/http-exception")
def route_with_http_exception():
    raise HTTPException(status_code=400)


@app.get("/request-validation/{param}/")
def route_with_request_validation_exception(param: int):
    pass  # pragma: no cover


@app.get("/server-error")
def route_with_server_error():
    raise RuntimeError("Oops!")


def test_override_http_exception():
    response = client.get("/http-exception")
    assert response.status_code == 200
    assert response.json() == {"exception": "http-exception"}


def test_override_request_validation_exception():
    response = client.get("/request-validation/invalid")
    assert response.status_code == 200
    assert response.json() == {"exception": "request-validation"}


def test_override_server_error_exception_raises():
    with pytest.raises(RuntimeError):
        client.get("/server-error")


def test_override_server_error_exception_response():
    client = TestClient(app, raise_server_exceptions=False)
    response = client.get("/server-error")
    assert response.status_code == 500
    assert response.json() == {"exception": "server-error"}


def test_traceback_for_dependency_with_yield():
    client = TestClient(app, raise_server_exceptions=True)
    with pytest.raises(ValueError) as exc_info:
        client.get("/dependency-with-yield")
    last_frame = exc_info.traceback[-1]
    assert str(last_frame.path) == __file__
    assert last_frame.lineno == raise_value_error.__code__.co_firstlineno

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_exception_handlers.py do?
test_exception_handlers.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in test_exception_handlers.py?
test_exception_handlers.py defines 14 function(s): dependency_with_yield, http_exception_handler, raise_value_error, request_validation_exception_handler, route_with_http_exception, route_with_request_validation_exception, route_with_server_error, server_error_exception_handler, test_override_http_exception, test_override_request_validation_exception, and 4 more.
What does test_exception_handlers.py depend on?
test_exception_handlers.py imports 6 module(s): RequestValidationError, __init__.py, exceptions.py, pytest, starlette.responses, testclient.py.
Where is test_exception_handlers.py in the architecture?
test_exception_handlers.py is located at tests/test_exception_handlers.py (domain: FastAPI, subdomain: Routing, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free