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

Return JSON for InternalServerErrors #19

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ REST_FRAMEWORK = {
}
```

### 5XX

You need to add `json_internal_server_error_middleware` to your `MIDDLEWARES` for having JSON 5XXs.

```
MIDDLEWARE = [
..
'hipo_drf_exceptions.middlewares.json_internal_server_error_middleware',
]
```

### Settings

```json
HIPO_DRF_EXCEPTIONS_SETTINGS = {
"INTERNAL_SERVER_ERROR_FALLBACK_MESSAGE": "Something is technically wrong."
}
```

- `INTERNAL_SERVER_ERROR_FALLBACK_MESSAGE` *optional*

default: `'Our servers are unreachable at the moment. Please try again a few minutes later.'`


### Example Error Responses

#### Field Error
Expand Down
8 changes: 7 additions & 1 deletion hipo_drf_exceptions/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from rest_framework.exceptions import APIException
from rest_framework.status import HTTP_400_BAD_REQUEST
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_500_INTERNAL_SERVER_ERROR

from hipo_drf_exceptions import settings


class BaseAPIException(APIException):
status_code = HTTP_400_BAD_REQUEST


class InternalServerError(APIException):
status_code = HTTP_500_INTERNAL_SERVER_ERROR
default_detail = settings.INTERNAL_SERVER_ERROR_FALLBACK_MESSAGE
15 changes: 15 additions & 0 deletions hipo_drf_exceptions/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from hipo_drf_exceptions import handler
from hipo_drf_exceptions.exceptions import InternalServerError


def json_internal_server_error_middleware(get_response):

def middleware(request):
json_content_type = "application/json"
response = get_response(request)

if str(response.status_code).startswith("5") and request.content_type == json_content_type:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This converts all 5XX responses to 500. Should not we check the full status code instead of checking the first character?

response = handler(InternalServerError(), {})
return response

return middleware
9 changes: 9 additions & 0 deletions hipo_drf_exceptions/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings as django_settings
from django.utils.translation import gettext_lazy as _

HIPO_DRF_EXCEPTIONS_SETTINGS = getattr(django_settings, "HIPO_DRF_EXCEPTIONS_SETTINGS", {})

INTERNAL_SERVER_ERROR_FALLBACK_MESSAGE = HIPO_DRF_EXCEPTIONS_SETTINGS.get(
"INTERNAL_SERVER_ERROR_FALLBACK_MESSAGE",
_('Our servers are unreachable at the moment. Please try again a few minutes later.')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to import gettext_lazy as _ for the translation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, fixed.

)