Skip to content

Commit

Permalink
Switch out PermissionsError for reraise with extended message
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelacey committed Jan 23, 2023
1 parent 05c11a4 commit eb42db1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
dict(e=exceptions.FieldError("test")),
dict(e=exceptions.NamingThingsError("test")),
dict(e=exceptions.NotFound("test")),
dict(e=exceptions.PermissionsError("test")),
dict(e=exceptions.WorfError("test")),
)
def test_exception(e):
Expand Down
5 changes: 0 additions & 5 deletions worf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class NotFound(WorfError):
message: str = "Not found"


@dataclass(frozen=True)
class PermissionsError(WorfError):
message: str


@dataclass(frozen=True)
class FieldError(WorfError, ValueError):
message: str
13 changes: 7 additions & 6 deletions worf/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

def browsable_response(request, response, status_code, view):
template = "worf/api.html"
serializer = view.get_serializer()
serializer = hasattr(view, "bundle") and view.get_serializer()

include = field_list(view.bundle.get("include", []))
search = field_list(view.bundle.get("search", []), delimiter="__")
bundle = getattr(view, "bundle", {})
include = field_list(bundle.get("include", []))
search = field_list(bundle.get("search", []), delimiter="__")

filter_fields = [
(transform_field(field), bool(field in view.bundle))
(transform_field(field), bool(field in bundle))
for field in getattr(view, "filter_fields", [])
]
include_fields = [
Expand Down Expand Up @@ -42,11 +43,11 @@ def browsable_response(request, response, status_code, view):
(
"Search",
sorted(search_fields),
len(search or search_fields) if view.bundle.get("q") else 0,
len(search or search_fields) if bundle.get("q") else 0,
),
],
lookup_kwargs=getattr(view, "lookup_kwargs", {}),
payload=view.bundle,
payload=bundle,
response=response,
serializer=serializer,
serializer_name=type(serializer).__name__,
Expand Down
1 change: 1 addition & 0 deletions worf/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from worf.views.create import CreateAPI # noqa: F401
from worf.views.delete import DeleteAPI # noqa: F401
from worf.views.detail import DetailAPI, DetailUpdateAPI # noqa: F401
from worf.views.errors import NotFound # noqa: F401
from worf.views.list import ListAPI, ListCreateAPI # noqa: F401
from worf.views.update import UpdateAPI # noqa: F401
17 changes: 6 additions & 11 deletions worf/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
DataConflict,
FieldError,
NotFound,
PermissionsError,
WorfError,
)
from worf.renderers import render_response
Expand All @@ -44,9 +43,9 @@ def render_to_response(self, data=None, status_code=200):
data = self.serialize()

if data is None:
msg = f"{self.codepath} did not pass an object to "
msg += "render_to_response, nor did its serializer method"
raise ImproperlyConfigured(msg)
message = f"{self.codepath} did not pass an object to "
message += "render_to_response, nor did its serializer method"
raise ImproperlyConfigured(message)

return render_response(self.request, data, status_code, self)

Expand All @@ -59,9 +58,6 @@ class AbstractBaseAPI(SerializeModels, ValidateFields, APIResponse):
def __init__(self, *args, **kwargs):
self.codepath = f"{self.__module__}.{self.__class__.__name__}"

if self.model is None: # pragma: no cover
raise ImproperlyConfigured(f"Model is not set on {self.codepath}")

if not isinstance(self.permissions, list): # pragma: no cover
raise ImproperlyConfigured(
f"{self.codepath}.permissions must be type: list"
Expand Down Expand Up @@ -127,10 +123,9 @@ def check_permissions(self):
perm()(self.request, **self.kwargs)
except WorfError as e:
if settings.WORF_DEBUG: # pragma: no cover
raise PermissionsError(
f"Permission check {perm.__module__}.{perm.__name__} raised {e.__class__.__name__}. "
f"You'd normally see a 4xx here but WORF_DEBUG=True."
) from e
check = f"{perm.__module__}.{perm.__name__}"
error = e.__class__.__name__
raise type(e)(f"{e.message}: Permission {check} raised {error}") from e
raise e

def get_handler(self, request, *args, **kwargs):
Expand Down
11 changes: 11 additions & 0 deletions worf/views/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from worf import exceptions
from worf.views.base import AbstractBaseAPI


@method_decorator(csrf_exempt, name="dispatch")
class NotFound(AbstractBaseAPI):
def perform_dispatch(self, *args, **kwargs):
raise exceptions.NotFound()

0 comments on commit eb42db1

Please sign in to comment.