Skip to content

๐Ÿšš โŒ

๐Ÿ“ค ๐Ÿ“š โš  ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšจ โŒ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ โš™๏ธ ๐Ÿ‘† ๐Ÿ› ๏ธ.

๐Ÿ‘‰ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ’ช ๐Ÿ–ฅ โฎ๏ธ ๐Ÿ•ธ, ๐Ÿ“Ÿ โšช๏ธโžก๏ธ ๐Ÿ‘ฑ ๐Ÿ™†, โ˜ ๐Ÿ“ณ, โ™’๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ช ๐Ÿ’ฌ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ:

  • ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿšซ โœ”๏ธ ๐Ÿฅƒ ๐Ÿ˜Œ ๐Ÿ‘ˆ ๐Ÿ› ๏ธ.
  • ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿšซ โœ”๏ธ ๐Ÿ” ๐Ÿ‘ˆ โ„น.
  • ๐Ÿฌ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”„ ๐Ÿ” ๐Ÿšซ ๐Ÿ”€.
  • โ™’๏ธ.

๐Ÿ‘ซ ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ›Ž ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ โ†” 4๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ (โšช๏ธโžก๏ธ 4๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ 4๏ธโƒฃ9๏ธโƒฃ9๏ธโƒฃ).

๐Ÿ‘‰ ๐ŸŽ 2๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ (โšช๏ธโžก๏ธ 2๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ 2๏ธโƒฃ9๏ธโƒฃ9๏ธโƒฃ). ๐Ÿ‘ˆ "2๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ" ๐Ÿ‘” ๐Ÿ“Ÿ โ›“ ๐Ÿ‘ˆ ๐Ÿ˜ซ ๐Ÿ“ค "๐Ÿ†" ๐Ÿ“จ.

๐Ÿ‘” ๐Ÿ“Ÿ 4๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ โ†” โ›“ ๐Ÿ‘ˆ ๐Ÿ“ค โŒ โšช๏ธโžก๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป.

๐Ÿ’ญ ๐ŸŒ ๐Ÿ‘ˆ "4๏ธโƒฃ0๏ธโƒฃ4๏ธโƒฃ ๐Ÿšซ ๐Ÿ”Ž" โŒ (& ๐Ÿคฃ) โ“

โš™๏ธ HTTPException

๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ“จ โฎ๏ธ โŒ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘† โš™๏ธ HTTPException.

๐Ÿ—„ HTTPException

from fastapi import FastAPI, HTTPException

app = FastAPI()

items = {"foo": "The Foo Wrestlers"}


@app.get("/items/{item_id}")
async def read_item(item_id: str):
    if item_id not in items:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"item": items[item_id]}

๐Ÿคš HTTPException ๐Ÿ‘† ๐Ÿ“Ÿ

HTTPException ๐Ÿ˜ ๐Ÿ โš  โฎ๏ธ ๐ŸŒ– ๐Ÿ“Š ๐Ÿ”— ๐Ÿ”—.

โ†ฉ๏ธ โšซ๏ธ ๐Ÿ โš , ๐Ÿ‘† ๐Ÿšซ return โšซ๏ธ, ๐Ÿ‘† raise โšซ๏ธ.

๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ”˜ ๐Ÿš™ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿค™ ๐Ÿ”˜ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, & ๐Ÿ‘† ๐Ÿคš HTTPException โšช๏ธโžก๏ธ ๐Ÿ”˜ ๐Ÿ‘ˆ ๐Ÿš™ ๐Ÿ”ข, โšซ๏ธ ๐Ÿ† ๐Ÿšซ ๐Ÿƒ ๐ŸŽ‚ ๐Ÿ“Ÿ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, โšซ๏ธ ๐Ÿ”œ โŽ ๐Ÿ‘ˆ ๐Ÿ“จ โ–ถ๏ธ๏ธ โ†–๏ธ & ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” โŒ โšช๏ธโžก๏ธ HTTPException ๐Ÿ‘ฉโ€๐Ÿ’ป.

๐Ÿ’ฐ ๐Ÿ™‹โ€โ™€ โš  ๐Ÿคญ return๐Ÿ˜… ๐Ÿ’ฒ ๐Ÿ”œ ๐ŸŒ– โญ ๐Ÿ“„ ๐Ÿ”ƒ ๐Ÿ”— & ๐Ÿ’‚โ€โ™‚.

๐Ÿ‘‰ ๐Ÿ–ผ, ๐Ÿ•โ” ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ ๐Ÿฌ ๐Ÿ†” ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ”€, ๐Ÿคš โš  โฎ๏ธ ๐Ÿ‘” ๐Ÿ“Ÿ 404:

from fastapi import FastAPI, HTTPException

app = FastAPI()

items = {"foo": "The Foo Wrestlers"}


@app.get("/items/{item_id}")
async def read_item(item_id: str):
    if item_id not in items:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"item": items[item_id]}

๐Ÿ“‰ ๐Ÿ“จ

๐Ÿšฅ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ http://example.com/items/foo ( item_id "foo"), ๐Ÿ‘ˆ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ 2๏ธโƒฃ0๏ธโƒฃ0๏ธโƒฃ, & ๐ŸŽป ๐Ÿ“จ:

{
  "item": "The Foo Wrestlers"
}

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ http://example.com/items/bar (๐Ÿšซ-๐Ÿšซ item_id "bar"), ๐Ÿ‘ˆ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ 4๏ธโƒฃ0๏ธโƒฃ4๏ธโƒฃ ("๐Ÿšซ ๐Ÿ”Ž" โŒ), & ๐ŸŽป ๐Ÿ“จ:

{
  "detail": "Item not found"
}

Tip

๐Ÿ•โ” ๐Ÿ™‹โ€โ™€ HTTPException, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐Ÿ™† ๐Ÿ’ฒ ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ—œ ๐ŸŽป ๐Ÿ”ข detail, ๐Ÿšซ ๐Ÿ•ด str.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ dict, list, โ™’๏ธ.

๐Ÿ‘ซ ๐Ÿต ๐Ÿ” FastAPI & ๐Ÿ—œ ๐ŸŽป.

๐Ÿšฎ ๐Ÿ›ƒ ๐ŸŽš

๐Ÿ“ค โš  ๐ŸŒโ” โšซ๏ธ โš  ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ›ƒ ๐ŸŽš ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” โŒ. ๐Ÿ–ผ, ๐Ÿ†Ž ๐Ÿ’‚โ€โ™‚.

๐Ÿ‘† ๐ŸŽฒ ๐Ÿ† ๐Ÿšซ ๐Ÿ’ช โš™๏ธ โšซ๏ธ ๐Ÿ”— ๐Ÿ‘† ๐Ÿ“Ÿ.

โœ‹๏ธ ๐Ÿ’ผ ๐Ÿ‘† ๐Ÿ’ช โšซ๏ธ ๐Ÿง ๐Ÿ˜, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ›ƒ ๐ŸŽš:

from fastapi import FastAPI, HTTPException

app = FastAPI()

items = {"foo": "The Foo Wrestlers"}


@app.get("/items-header/{item_id}")
async def read_item_header(item_id: str):
    if item_id not in items:
        raise HTTPException(
            status_code=404,
            detail="Item not found",
            headers={"X-Error": "There goes my error"},
        )
    return {"item": items[item_id]}

โŽ ๐Ÿ›ƒ โš  ๐Ÿ•โ€๐Ÿฆบ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ›ƒ โš  ๐Ÿ•โ€๐Ÿฆบ โฎ๏ธ ๐ŸŽ โš  ๐Ÿš™ โšช๏ธโžก๏ธ ๐Ÿ’ƒ.

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† โœ”๏ธ ๐Ÿ›ƒ โš  UnicornException ๐Ÿ‘ˆ ๐Ÿ‘† (โš–๏ธ ๐Ÿ—ƒ ๐Ÿ‘† โš™๏ธ) ๐Ÿ’ช raise.

& ๐Ÿ‘† ๐Ÿ’š ๐Ÿต ๐Ÿ‘‰ โš  ๐ŸŒ โฎ๏ธ FastAPI.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ›ƒ โš  ๐Ÿ•โ€๐Ÿฆบ โฎ๏ธ @app.exception_handler():

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse


class UnicornException(Exception):
    def __init__(self, name: str):
        self.name = name


app = FastAPI()


@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
    return JSONResponse(
        status_code=418,
        content={"message": f"Oops! {exc.name} did something. There goes a rainbow..."},
    )


@app.get("/unicorns/{name}")
async def read_unicorn(name: str):
    if name == "yolo":
        raise UnicornException(name=name)
    return {"unicorn_name": name}

๐Ÿ“ฅ, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“จ /unicorns/yolo, โžก ๐Ÿ› ๏ธ ๐Ÿ”œ raise UnicornException.

โœ‹๏ธ โšซ๏ธ ๐Ÿ”œ ๐Ÿต unicorn_exception_handler.

, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ“จ ๐Ÿงน โŒ, โฎ๏ธ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ 418 & ๐ŸŽป ๐ŸŽš:

{"message": "Oops! yolo did something. There goes a rainbow..."}

๐Ÿ“ก โ„น

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ from starlette.requests import Request & from starlette.responses import JSONResponse.

FastAPI ๐Ÿšš ๐ŸŽ starlette.responses fastapi.responses ๐Ÿช ๐Ÿ‘†, ๐Ÿ‘ฉโ€๐Ÿ’ป. โœ‹๏ธ ๐ŸŒ… ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ’ƒ. ๐ŸŽ โฎ๏ธ Request.

๐Ÿ” ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ

FastAPI โœ”๏ธ ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ.

๐Ÿ‘ซ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿˆš ๐Ÿ›ฌ ๐Ÿ”ข ๐ŸŽป ๐Ÿ“จ ๐Ÿ•โ” ๐Ÿ‘† raise HTTPException & ๐Ÿ•โ” ๐Ÿ“จ โœ”๏ธ โŒ ๐Ÿ’ฝ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ” ๐Ÿ‘ซ โš  ๐Ÿ•โ€๐Ÿฆบ โฎ๏ธ ๐Ÿ‘† ๐Ÿ‘.

๐Ÿ” ๐Ÿ“จ ๐Ÿ”ฌ โš 

๐Ÿ•โ” ๐Ÿ“จ ๐Ÿ”Œ โŒ ๐Ÿ“Š, FastAPI ๐Ÿ”˜ ๐Ÿคš RequestValidationError.

& โšซ๏ธ ๐Ÿ”Œ ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ โšซ๏ธ.

๐Ÿ” โšซ๏ธ, ๐Ÿ—„ RequestValidationError & โš™๏ธ โšซ๏ธ โฎ๏ธ @app.exception_handler(RequestValidationError) ๐ŸŽ€ โš  ๐Ÿ•โ€๐Ÿฆบ.

โš  ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”œ ๐Ÿ“จ Request & โš .

from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from starlette.exceptions import HTTPException as StarletteHTTPException

app = FastAPI()


@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
    return PlainTextResponse(str(exc.detail), status_code=exc.status_code)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    return PlainTextResponse(str(exc), status_code=400)


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}

๐Ÿ”œ, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿšถ /items/foo, โ†ฉ๏ธ ๐Ÿ’†โ€โ™‚ ๐Ÿ”ข ๐ŸŽป โŒ โฎ๏ธ:

{
    "detail": [
        {
            "loc": [
                "path",
                "item_id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}

๐Ÿ‘† ๐Ÿ”œ ๐Ÿคš โœ โฌ, โฎ๏ธ:

1 validation error
path -> item_id
  value is not a valid integer (type=type_error.integer)

RequestValidationError ๐Ÿ†š ValidationError

Warning

๐Ÿ‘ซ ๐Ÿ“ก โ„น ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถ ๐Ÿšฅ โšซ๏ธ ๐Ÿšซ โš  ๐Ÿ‘† ๐Ÿ”œ.

RequestValidationError ๐ŸŽง-๐ŸŽ“ Pydantic ValidationError.

FastAPI โš™๏ธ โšซ๏ธ ๐Ÿ‘ˆ, ๐Ÿšฅ ๐Ÿ‘† โš™๏ธ Pydantic ๐Ÿท response_model, & ๐Ÿ‘† ๐Ÿ’ฝ โœ”๏ธ โŒ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โŒ ๐Ÿ‘† ๐Ÿ•น.

โœ‹๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป/๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”œ ๐Ÿšซ ๐Ÿ‘€ โšซ๏ธ. โ†ฉ๏ธ, ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”œ ๐Ÿ“จ "๐Ÿ”— ๐Ÿ’ฝ โŒ" โฎ๏ธ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘” ๐Ÿ“Ÿ 500.

โšซ๏ธ ๐Ÿ”œ ๐Ÿ‘‰ ๐ŸŒŒ โ†ฉ๏ธ ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ Pydantic ValidationError ๐Ÿ‘† ๐Ÿ“จ โš–๏ธ ๐Ÿ™† ๐Ÿ‘† ๐Ÿ“Ÿ (๐Ÿšซ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ), โšซ๏ธ ๐Ÿค™ ๐Ÿ› ๐Ÿ‘† ๐Ÿ“Ÿ.

& โช ๐Ÿ‘† ๐Ÿ”ง โšซ๏ธ, ๐Ÿ‘† ๐Ÿ‘ฉโ€๐Ÿ’ป/๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿšซ๐Ÿ”œ ๐Ÿšซ โœ”๏ธ ๐Ÿ” ๐Ÿ”— โ„น ๐Ÿ”ƒ โŒ, ๐Ÿ‘ˆ ๐Ÿ’ช ๐ŸŽฆ ๐Ÿ’‚โ€โ™‚ โš .

๐Ÿ” HTTPException โŒ ๐Ÿ•โ€๐Ÿฆบ

๐ŸŽ ๐ŸŒŒ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ” HTTPException ๐Ÿ•โ€๐Ÿฆบ.

๐Ÿ–ผ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿ“จ โœ… โœ ๐Ÿ“จ โ†ฉ๏ธ ๐ŸŽป ๐Ÿ‘ซ โŒ:

from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
from starlette.exceptions import HTTPException as StarletteHTTPException

app = FastAPI()


@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
    return PlainTextResponse(str(exc.detail), status_code=exc.status_code)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    return PlainTextResponse(str(exc), status_code=400)


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}

๐Ÿ“ก โ„น

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ from starlette.responses import PlainTextResponse.

FastAPI ๐Ÿšš ๐ŸŽ starlette.responses fastapi.responses ๐Ÿช ๐Ÿ‘†, ๐Ÿ‘ฉโ€๐Ÿ’ป. โœ‹๏ธ ๐ŸŒ… ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ’ƒ.

โš™๏ธ RequestValidationError ๐Ÿ’ช

RequestValidationError ๐Ÿ”Œ body โšซ๏ธ ๐Ÿ“จ โฎ๏ธ โŒ ๐Ÿ’ฝ.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โšซ๏ธ โช ๐Ÿ› ๏ธ ๐Ÿ‘† ๐Ÿ“ฑ ๐Ÿ•น ๐Ÿ’ช & โ„น โšซ๏ธ, ๐Ÿ“จ โšซ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป, โ™’๏ธ.

from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel

app = FastAPI()


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    return JSONResponse(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
    )


class Item(BaseModel):
    title: str
    size: int


@app.post("/items/")
async def create_item(item: Item):
    return item

๐Ÿ”œ ๐Ÿ”„ ๐Ÿ“จ โŒ ๐Ÿฌ ๐Ÿ’–:

{
  "title": "towel",
  "size": "XL"
}

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿ‘ˆ ๐Ÿ’ฝ โŒ โš— ๐Ÿ“จ ๐Ÿ’ช:

{
  "detail": [
    {
      "loc": [
        "body",
        "size"
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ],
  "body": {
    "title": "towel",
    "size": "XL"
  }
}

FastAPI HTTPException ๐Ÿ†š ๐Ÿ’ƒ HTTPException

FastAPI โœ”๏ธ ๐Ÿšฎ ๐Ÿ‘ HTTPException.

& FastAPI'โ“‚ HTTPException โŒ ๐ŸŽ“ ๐Ÿ˜– โšช๏ธโžก๏ธ ๐Ÿ’ƒ HTTPException โŒ ๐ŸŽ“.

๐Ÿ•ด ๐Ÿ”บ, ๐Ÿ‘ˆ FastAPI'โ“‚ HTTPException โœ” ๐Ÿ‘† ๐Ÿšฎ ๐ŸŽš ๐Ÿ”Œ ๐Ÿ“จ.

๐Ÿ‘‰ ๐Ÿ’ช/โš™๏ธ ๐Ÿ”˜ โœณ 2๏ธโƒฃ.0๏ธโƒฃ & ๐Ÿ’‚โ€โ™‚ ๐Ÿš™.

, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšง ๐Ÿ™‹โ€โ™€ FastAPI'โ“‚ HTTPException ๐Ÿ›Ž ๐Ÿ‘† ๐Ÿ“Ÿ.

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ‘† ยฎ โš  ๐Ÿ•โ€๐Ÿฆบ, ๐Ÿ‘† ๐Ÿ”œ ยฎ โšซ๏ธ ๐Ÿ’ƒ HTTPException.

๐Ÿ‘‰ ๐ŸŒŒ, ๐Ÿšฅ ๐Ÿ™† ๐Ÿ• ๐Ÿ’ƒ ๐Ÿ”— ๐Ÿ“Ÿ, โš–๏ธ ๐Ÿ’ƒ โ†” โš–๏ธ ๐Ÿ”Œ -, ๐Ÿคš ๐Ÿ’ƒ HTTPException, ๐Ÿ‘† ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ”œ ๐Ÿ’ช โœŠ & ๐Ÿต โšซ๏ธ.

๐Ÿ‘‰ ๐Ÿ–ผ, ๐Ÿ’ช โœ”๏ธ ๐Ÿ‘ฏโ€โ™‚๏ธ HTTPExceptionโ“‚ ๐ŸŽ ๐Ÿ“Ÿ, ๐Ÿ’ƒ โš  ๐Ÿ“ StarletteHTTPException:

from starlette.exceptions import HTTPException as StarletteHTTPException

๐Ÿค-โš™๏ธ FastAPI'โ“‚ โš  ๐Ÿ•โ€๐Ÿฆบ

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š โš™๏ธ โš  โคด๏ธ โฎ๏ธ ๐ŸŽ ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ โšช๏ธโžก๏ธ FastAPI, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ—„ & ๐Ÿค-โš™๏ธ ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ โšช๏ธโžก๏ธ fastapi.exception_handlers:

from fastapi import FastAPI, HTTPException
from fastapi.exception_handlers import (
    http_exception_handler,
    request_validation_exception_handler,
)
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException

app = FastAPI()


@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
    print(f"OMG! An HTTP error!: {repr(exc)}")
    return await http_exception_handler(request, exc)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    print(f"OMG! The client sent invalid data!: {exc}")
    return await request_validation_exception_handler(request, exc)


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}

๐Ÿ‘‰ ๐Ÿ–ผ ๐Ÿ‘† print๐Ÿ˜… โŒ โฎ๏ธ ๐Ÿ“ถ ๐ŸŽจ ๐Ÿ“ง, โœ‹๏ธ ๐Ÿ‘† ๐Ÿคš ๐Ÿ’ญ. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โš  & โคด๏ธ ๐Ÿค-โš™๏ธ ๐Ÿ”ข โš  ๐Ÿ•โ€๐Ÿฆบ.