-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Improve Exception handlers #6430
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
370a1b4
feat: modify error handlers
montezdesousa f561da2
Merge branch 'develop' into feature/error-handling
montezdesousa 0d2669e
fix: clean python errors
montezdesousa 87ff930
fix: return error kind
montezdesousa 5bd73be
fix: clean decorator
montezdesousa ada7a42
fix: query validation error kind
montezdesousa 513ffd8
fix: no kind, API
montezdesousa a20e04d
fix: no kind, python
montezdesousa 4991c9c
fix: rename error
montezdesousa ab45c92
lint
montezdesousa b3e1175
Merge branch 'develop' into feature/error-handling
montezdesousa 381f402
update docs
montezdesousa c47da94
comment
montezdesousa e495eda
typo
montezdesousa 1df6b65
rename handler
montezdesousa a329b93
rename base handler
montezdesousa a247998
Merge branch 'develop' into feature/error-handling
IgorWounds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
69 changes: 69 additions & 0 deletions
69
openbb_platform/core/openbb_core/api/exception_handlers.py
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,69 @@ | ||
"""Exception handlers module.""" | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from fastapi import Request | ||
from fastapi.responses import JSONResponse | ||
from openbb_core.app.model.abstract.error import OpenBBError | ||
from openbb_core.env import Env | ||
from pydantic import ValidationError | ||
|
||
logger = logging.getLogger("uvicorn.error") | ||
|
||
|
||
class ExceptionHandlers: | ||
"""Exception handlers.""" | ||
|
||
@staticmethod | ||
async def _handle(exception: Exception, status_code: int, detail: Any): | ||
"""Exception handler.""" | ||
if Env().DEBUG_MODE: | ||
raise exception | ||
logger.error(exception) | ||
return JSONResponse( | ||
status_code=status_code, | ||
content={ | ||
"detail": detail, | ||
}, | ||
) | ||
|
||
@staticmethod | ||
async def exception(_: Request, error: Exception) -> JSONResponse: | ||
"""Exception handler for Base Exception.""" | ||
return await ExceptionHandlers._handle( | ||
exception=error, | ||
status_code=500, | ||
detail="Unexpected error.", | ||
) | ||
|
||
@staticmethod | ||
async def validation(request: Request, error: ValidationError): | ||
"""Exception handler for ValidationError.""" | ||
# Some validation is performed at Fetcher level. | ||
# So we check if the validation error comes from a QueryParams class. | ||
# And that it is in the request query params. | ||
# If yes, we update the error location with query. | ||
# If not, we handle it as a base Exception error. | ||
query_params = dict(request.query_params) | ||
errors = error.errors(include_url=False) | ||
all_in_query = all( | ||
loc in query_params for err in errors for loc in err.get("loc", ()) | ||
) | ||
if "QueryParams" in error.title and all_in_query: | ||
detail = [{**err, "loc": ("query",) + err.get("loc", ())} for err in errors] | ||
return await ExceptionHandlers._handle( | ||
exception=error, | ||
status_code=422, | ||
detail=detail, | ||
) | ||
return await ExceptionHandlers.exception(request, error) | ||
|
||
@staticmethod | ||
async def openbb(_: Request, error: OpenBBError): | ||
"""Exception handler for OpenBBError.""" | ||
return await ExceptionHandlers._handle( | ||
exception=error, | ||
status_code=400, | ||
detail=str(error.original), | ||
) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go to the docs website.
We should consider deprecate this document and just point potential contributors to the website instead.
cc @IgorWounds @montezdesousa