Skip to content
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

New class layer #349

Merged
merged 13 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,229 changes: 1,081 additions & 148 deletions jai/core/base.py

Large diffs are not rendered by default.

55 changes: 1 addition & 54 deletions jai/core/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import functools

from .exceptions import DeprecatedError, ParamError, ValidationError
from .exceptions import DeprecatedError


def deprecated(func):
Expand All @@ -10,56 +10,3 @@ def inner(*args, **kwargs):
raise DeprecatedError(error_str)

return inner


def raise_status_error(code):
"""
Decorator to process responses with unexpected response codes.

Args
----
code: int
Expected Code.

"""

def decorator(function):
@functools.wraps(function)
def new_function(*args, **kwargs):
response = function(*args, **kwargs)
if response.status_code == code:
return response.json()
# find a way to process this
# what errors to raise, etc.
message = f"Something went wrong.\n\nSTATUS: {response.status_code}\n"
try:
res_json = response.json()
if isinstance(res_json, dict):
detail = res_json.get(
"message", res_json.get("detail", response.text)
)
else:
detail = response.text
except:
detail = response.text

detail = str(detail)

if "Error: " in detail:
error, msg = detail.split(": ", 1)
try:
raise eval(error)(message + msg)
except:
if error == "DeprecatedError":
raise DeprecatedError(message + msg)
elif error == "ValidationError":
raise ValidationError(message + msg)
elif error == "ParamError":
raise ParamError(message + msg)
raise BaseException(message + response.text)
else:
raise ValueError(message + detail)

return new_function

return decorator
Loading