From 04662ba7f7ef44ed136abb3a45bd4d4082847f2f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 4 Sep 2025 12:09:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20format?= =?UTF-8?q?=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/handling_errors/tutorial002.py | 1 - docs_src/handling_errors/tutorial004.py | 18 ++++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/docs_src/handling_errors/tutorial002.py b/docs_src/handling_errors/tutorial002.py index e4d388e24..e48c295c9 100644 --- a/docs_src/handling_errors/tutorial002.py +++ b/docs_src/handling_errors/tutorial002.py @@ -7,7 +7,6 @@ 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, diff --git a/docs_src/handling_errors/tutorial004.py b/docs_src/handling_errors/tutorial004.py index 8c2c3dec7..d95bd01cb 100644 --- a/docs_src/handling_errors/tutorial004.py +++ b/docs_src/handling_errors/tutorial004.py @@ -1,9 +1,9 @@ from fastapi import FastAPI, HTTPException, Request +from fastapi.encoders import jsonable_encoder from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse -from starlette.exceptions import HTTPException as StarletteHTTPException -from fastapi.encoders import jsonable_encoder from starlette import status +from starlette.exceptions import HTTPException as StarletteHTTPException app = FastAPI() @@ -14,10 +14,8 @@ async def http_exception_handler(request: Request, exc: StarletteHTTPException): Handles all HTTPException (Starlette) errors. Returns a JSON response with status code and detail. """ - return JSONResponse( - status_code=exc.status_code, - content={"message": exc.detail} - ) + return JSONResponse(status_code=exc.status_code, content={"message": exc.detail}) + @app.exception_handler(RequestValidationError) async def validation_exception_handler(request: Request, exc: RequestValidationError): @@ -27,16 +25,12 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE """ return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, - content=jsonable_encoder({"errors": exc.errors(), "body": exc.body}) + content=jsonable_encoder({"errors": exc.errors(), "body": exc.body}), ) @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." - ) + raise HTTPException(status_code=418, detail="Nope! I don't like 3.") return {"item_id": item_id}