-
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.
adding docstrings and standardizing errors
- Loading branch information
1 parent
9960b42
commit c8ca5b4
Showing
8 changed files
with
125 additions
and
81 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
from ._catch_flask_errors import catch_exception_and_abort, server_error_responses | ||
|
||
__all__ = ["catch_exception_and_abort", "server_error_responses"] |
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,27 @@ | ||
import contextlib | ||
|
||
import flask_restx | ||
from tpying import List, Union | ||
|
||
|
||
def server_error_responses(*, codes: List[str]) -> Dict[int, str]: | ||
all_server_error_responses = { | ||
200: "Success", | ||
400: "Bad request", | ||
404: "Resource is not accessible.", | ||
500: ("Internalerver error"), | ||
} | ||
|
||
selected_responses = {code: all_server_error_responses[code] for code in codes} | ||
return selected_responses | ||
|
||
|
||
@contextlib.contextmanager | ||
def catch_exception_and_abort(*, api: Union[flask_restx.Api, flask_restx.Namespace], code: int) -> None: | ||
try: | ||
yield | ||
except Exception as exception: | ||
exception_type = type(exception) | ||
exception_message = str(exception) | ||
api.abort(code=code, message=f"{exception_type}: {exception_message}") | ||
raise exception |