mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-31 10:10:51 -05:00
* 🌐 Refactor file structure to support internationalization * ✅ Update tests changed after i18n * 🔀 Merge Typer style from master * 🔧 Update MkConfig with Typer-styles * 🎨 Format mkdocs.yml with cannonical form * 🎨 Format mkdocs.yml * 🔧 Update MkDocs config * ➕ Add docs translation scripts dependencies * ✨ Add Typer scripts to handle translations * ✨ Add missing translation snippet to include * ✨ Update contributing docs, add docs for translations * 🙈 Add docs_build to gitignore * 🔧 Update scripts with new locations and docs scripts * 👷 Update docs deploy action with translations * 📝 Add note about languages not supported in the theme * ✨ Add first translation, for Spanish
29 lines
922 B
Python
29 lines
922 B
Python
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!: {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}
|