forked from QuivrHQ/quivr
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: improve error handling * docs: explain error handling system
- Loading branch information
1 parent
59fe7b0
commit 3922d8c
Showing
8 changed files
with
155 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,39 +5,50 @@ | |
from auth.jwt_token_handler import decode_access_token, verify_token | ||
from fastapi import Depends, HTTPException, Request | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from models.settings import CommonsDep | ||
from models.users import User | ||
|
||
|
||
class AuthBearer(HTTPBearer): | ||
def __init__(self, auto_error: bool = True): | ||
super().__init__(auto_error=auto_error) | ||
|
||
async def __call__(self, request: Request, commons: CommonsDep): | ||
async def __call__( | ||
self, | ||
request: Request, | ||
): | ||
credentials: Optional[HTTPAuthorizationCredentials] = await super().__call__( | ||
request | ||
) | ||
self.check_scheme(credentials) | ||
token = credentials.credentials | ||
return await self.authenticate(token, commons) | ||
return await self.authenticate( | ||
token, | ||
) | ||
|
||
def check_scheme(self, credentials): | ||
if credentials and not credentials.scheme == "Bearer": | ||
raise HTTPException(status_code=402, detail="Invalid authorization scheme.") | ||
if credentials and credentials.scheme != "Bearer": | ||
raise HTTPException(status_code=401, detail="Token must be Bearer") | ||
elif not credentials: | ||
raise HTTPException(status_code=403, detail="Invalid authorization code.") | ||
raise HTTPException( | ||
status_code=403, detail="Authentication credentials missing" | ||
) | ||
|
||
async def authenticate(self, token: str, commons: CommonsDep): | ||
async def authenticate( | ||
self, | ||
token: str, | ||
): | ||
if os.environ.get("AUTHENTICATE") == "false": | ||
return self.get_test_user() | ||
elif verify_token(token): | ||
return decode_access_token(token) | ||
elif await verify_api_key(token, commons): | ||
return await get_user_from_api_key(token, commons) | ||
else: | ||
raise HTTPException( | ||
status_code=402, detail="Invalid token or expired token." | ||
elif await verify_api_key( | ||
token, | ||
): | ||
return await get_user_from_api_key( | ||
token, | ||
) | ||
else: | ||
raise HTTPException(status_code=401, detail="Invalid token or api key.") | ||
|
||
def get_test_user(self): | ||
return {"email": "[email protected]"} # replace with test user information | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Error Handling | ||
|
||
**URL**: https://api.quivr.app/chat | ||
|
||
**Swagger**: https://api.quivr.app/docs | ||
|
||
## Overview | ||
|
||
This page provides information about common error codes, their descriptions, and examples of scenarios where these errors may occur. | ||
|
||
| Error Code | Description | | ||
| ---------- | --------------------------------------------------------------------------- | | ||
| 401 | Unauthorized: The request lacks valid authentication credentials. | | ||
| 403 | Forbidden: The requested operation is not allowed. | | ||
| 422 | Unprocessable Entity: The request is well-formed but contains invalid data. | | ||
| 500 | Internal Server Error: An unexpected error occurred on the server. | | ||
|
||
## Error Code: 401 | ||
|
||
**Description**: The request lacks valid authentication credentials or the provided token/api key is invalid. | ||
|
||
Example Scenarios: | ||
|
||
- Missing or invalid authentication token/api key. | ||
- Expired authentication token. | ||
|
||
## Error Code: 403 | ||
|
||
**Description**: The requested operation is forbidden due to insufficient privileges or credentials missing. | ||
|
||
Example Scenarios: | ||
|
||
- Attempting to access a resource without proper authorization. | ||
- Insufficient permissions to perform a specific action. | ||
|
||
## Error Code: 422 | ||
|
||
**Description**: The request is well-formed, but contains invalid data or parameters. | ||
|
||
Example Scenarios: | ||
|
||
- Invalid input data format. | ||
- Required fields are missing or have incorrect values. | ||
|
||
## Error Code: 500 | ||
|
||
**Description**: An unexpected error occurred on the server. | ||
|
||
Example Scenarios: | ||
|
||
- Internal server error due to a server-side issue. | ||
- Unhandled exceptions or errors during request processing. |