Skip to content

Commit

Permalink
Move the exception handler to registry_api.py
Browse files Browse the repository at this point in the history
The exception handler should be closer to APIView classes where it is
actually being used. At the same time, this resolves the circular import
errors caused by the recent extraction of the get_content_data method.

closes #1561
  • Loading branch information
lubosmj committed Mar 20, 2024
1 parent 7ad4ee7 commit 64f427d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES/1561.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resolved circular import errors raised when using pulp-container as a library.
17 changes: 1 addition & 16 deletions pulp_container/app/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
from rest_framework import status, views
from rest_framework.exceptions import (
AuthenticationFailed,
NotAuthenticated,
NotFound,
ParseError,
)


def unauthorized_exception_handler(exc, context):
response = views.exception_handler(exc, context)

if isinstance(exc, (AuthenticationFailed, NotAuthenticated)):
response.status_code = status.HTTP_401_UNAUTHORIZED

return response
from rest_framework.exceptions import NotFound, ParseError


class RepositoryNotFound(NotFound):
Expand Down
13 changes: 11 additions & 2 deletions pulp_container/app/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
from rest_framework.serializers import ModelSerializer
from rest_framework.settings import api_settings
from rest_framework.viewsets import ViewSet
from rest_framework.views import APIView
from rest_framework.views import APIView, exception_handler
from rest_framework.status import HTTP_401_UNAUTHORIZED

from pulp_container.app import models, serializers
from pulp_container.app.authorization import AuthorizationService
Expand All @@ -53,7 +54,6 @@
RegistryApiCache,
)
from pulp_container.app.exceptions import (
unauthorized_exception_handler,
InvalidRequest,
RepositoryNotFound,
RepositoryInvalid,
Expand Down Expand Up @@ -417,6 +417,15 @@ def get_exception_handler(self):
return unauthorized_exception_handler


def unauthorized_exception_handler(exc, context):
response = exception_handler(exc, context)

if isinstance(exc, (AuthenticationFailed, NotAuthenticated)):
response.status_code = HTTP_401_UNAUTHORIZED

return response


class VersionView(ContainerRegistryApiMixin, APIView):
"""
Handles requests to the /v2/ endpoint.
Expand Down

0 comments on commit 64f427d

Please sign in to comment.