HTTP exception that you can raise to return a specific HTTP error response.
Since this is defined in the auth module, we default to a 401 status code.
HTTPException(
self,
status_code: int = 401,
detail: str | None = None,
headers: Mapping[str, str] | None = None
)Example:
Default:
raise HTTPException()
# HTTPException(status_code=401, detail='Unauthorized')
Add headers:
raise HTTPException(headers={"X-Custom-Header": "Custom Value"})
# HTTPException(status_code=401, detail='Unauthorized', headers={"WWW-Authenticate": "Bearer"})
Custom error:
raise HTTPException(status_code=404, detail="Not found")| Name | Type | Description |
|---|---|---|
status_code | int | Default: 401HTTP status code for the error. Defaults to 401 "Unauthorized". |
detail | str | None | Default: NoneDetailed error message. If |
headers | Mapping[str, str] | None | Default: NoneAdditional HTTP headers to include in the error response. |