FASTAPI 시작하기 (6) - 예외 처리

code_able·2023년 3월 12일
0

fastaip는 HTTPException라는 모듈을 사용하여
error를 리턴해 줄 수 있다.

githup에 올라온 보일러템플릿들을 보면 custom으로 만드는 경우도 많다.
우리는 아직 fastapi로 그렇게 큰 프로젝트를 하는 것은 아니다.
나중에 필요하면 추가 하도록 하겠다.

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]}
profile
할수 있다! code able

0개의 댓글