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

Remove exception construction #70

Merged
merged 2 commits into from
May 12, 2024
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
8 changes: 0 additions & 8 deletions drf_api_action/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
class ActionsAPIException(RuntimeError):
pass


class ActionsAPIExceptionMiddleware:
def __new__(cls, *args, **kwargs):
error_type = kwargs.pop('error_type', Exception)
error = error_type(*args)
error.__traceback__ = kwargs.pop('traceback', error_type.__traceback__)
return error
19 changes: 5 additions & 14 deletions drf_api_action/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Optional
from drf_api_action.exceptions import ActionsAPIExceptionMiddleware


class CustomRequest:
Expand Down Expand Up @@ -27,19 +26,11 @@ def run_as_api(self, func, serializer_class, *args, **kw):
self.kwargs = kw # adding our enhanced kwargs into instance kwargs
self.request = request # mocking request with our arguments as data in the instance

try:
ret = func(request, **kw)
if isinstance(ret.data, list): # multiple results
results = [dict(res) for res in ret.data]
else: # only one json
results = {k.lower(): v for k, v in ret.data.items()}
except Exception as error: # pylint: disable=broad-except
error_type = type(error)
# re-constructing the error with the actual traceback
raised_exception = ActionsAPIExceptionMiddleware(*error.args,
error_type=error_type,
traceback=error.__traceback__) # fixing stack frames
raise raised_exception # pylint: disable=raising-non-exception
ret = func(request, **kw) # evaluating endpoint
if isinstance(ret.data, list): # multiple results
results = [dict(res) for res in ret.data]
else: # only one json
results = {k.lower(): v for k, v in ret.data.items()}

return results

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "drf_api_action"
version = "1.2.1"
version = "1.2.2"
description = "drf-api-action elevates DRF testing by simplifying REST endpoint testing to a seamless, function-like experience."
readme = "README.md"
authors = ["Ori Roza <[email protected]>"]
Expand Down
Loading