Skip to content

Commit

Permalink
(chore) Add api fallback exception handler. (#430)
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Shimkus <[email protected]>
  • Loading branch information
jshimkus-rh authored Oct 17, 2023
1 parent a75a5a3 commit 68473e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/aap_eda/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework import status
from rest_framework.exceptions import (
Expand All @@ -20,6 +21,8 @@
NotFound,
PermissionDenied,
)
from rest_framework.response import Response
from rest_framework.views import exception_handler

__all__ = (
"AuthenticationFailed",
Expand All @@ -29,9 +32,26 @@
"Conflict",
"Unprocessable",
"PermissionDenied",
"api_fallback_handler",
)


def api_fallback_handler(exc, context):
response = exception_handler(exc, context)
if (response is None) and (not settings.DEBUG):
response = Response(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
data={
"detail": (
"Unexpected server error"
"; contact your system administrator"
)
},
)
response["context"] = context
return response


class BadRequest(APIException):
status = status.HTTP_400_BAD_REQUEST
default_code = "bad_request"
Expand Down
1 change: 1 addition & 0 deletions src/aap_eda/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def _get_secret_key() -> str:
"aap_eda.api.permissions.RoleBasedPermission",
],
"TEST_REQUEST_DEFAULT_FORMAT": "json",
"EXCEPTION_HANDLER": "aap_eda.api.exceptions.api_fallback_handler",
}

# ---------------------------------------------------------
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/api/test_fallback_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest import mock

import pytest
from rest_framework import status
from rest_framework.test import APIClient

from tests.integration.constants import api_url_v1


class FallbackException(Exception):
pass


def raise_exception(self, request):
raise FallbackException


@pytest.mark.django_db
@mock.patch(
"aap_eda.api.views.project.ProjectViewSet.list", new=raise_exception
)
def test_debug_unexpected_exception(client: APIClient, settings):
settings.DEBUG = True
with pytest.raises(FallbackException):
client.get(f"{api_url_v1}/projects/")


@pytest.mark.django_db
@mock.patch(
"aap_eda.api.views.project.ProjectViewSet.list", new=raise_exception
)
def test_non_debug_unexpected_exception(client: APIClient, settings):
settings.DEBUG = False
response = client.get(f"{api_url_v1}/projects/")
assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR

data = response.json()
assert data["detail"].startswith("Unexpected server error")

0 comments on commit 68473e7

Please sign in to comment.