mirror of
https://github.com/fastapi/fastapi.git
synced 2026-03-06 07:59:17 -05:00
✨ Add HTTPException with custom headers (#35)
* 📝 Update Release Notes with issue templates * ✨ Add HTTPException with support for headers Including docs and tests * 📝 Update Security docs to use new HTTPException
This commit is contained in:
committed by
GitHub
parent
7edbd9345b
commit
8772e2f2ee
@@ -5,3 +5,4 @@ __version__ = "0.4.0"
|
||||
from .applications import FastAPI
|
||||
from .routing import APIRouter
|
||||
from .params import Body, Path, Query, Header, Cookie, Form, File, Security, Depends
|
||||
from .exceptions import HTTPException
|
||||
|
||||
@@ -13,7 +13,13 @@ from starlette.responses import JSONResponse, Response
|
||||
|
||||
|
||||
async def http_exception(request: Request, exc: HTTPException) -> JSONResponse:
|
||||
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
|
||||
headers = getattr(exc, "headers", None)
|
||||
if headers:
|
||||
return JSONResponse(
|
||||
{"detail": exc.detail}, status_code=exc.status_code, headers=headers
|
||||
)
|
||||
else:
|
||||
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
|
||||
|
||||
|
||||
class FastAPI(Starlette):
|
||||
|
||||
9
fastapi/exceptions.py
Normal file
9
fastapi/exceptions.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
|
||||
class HTTPException(StarletteHTTPException):
|
||||
def __init__(
|
||||
self, status_code: int, detail: str = None, headers: dict = None
|
||||
) -> None:
|
||||
super().__init__(status_code=status_code, detail=detail)
|
||||
self.headers = headers
|
||||
Reference in New Issue
Block a user