Skip to content

Commit

Permalink
use json instead of html 404 page (#28)
Browse files Browse the repository at this point in the history
* use json instead of html 404 page

* force JSON response in exception handler
  • Loading branch information
fqrious authored Nov 28, 2024
1 parent f841835 commit e984496
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
7 changes: 2 additions & 5 deletions dogesec_commons/utils/autoschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ def add_default_pages(self, operation):
responses = operation['responses']

default_responses = {
'404': self._get_response_for_code(schemas.WEBSERVER_404_RESPONSE, '404', ["text/html"]),
'500': self._get_response_for_code(schemas.WEBSERVER_500_RESPONSE, '500', ["text/html"]),
'404': self._get_response_for_code(schemas.WEBSERVER_404_RESPONSE, '404', ["application/json"]),
}
for code, content_response in default_responses.items():
if code in responses:
responses[code]['content'].update(content_response['content'])
else:
if code not in responses:
responses[code] = content_response
return operation

Expand Down
2 changes: 2 additions & 0 deletions dogesec_commons/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework.views import exception_handler
from rest_framework.exceptions import ValidationError
from django.core import exceptions as django_exceptions
from django.http import JsonResponse



Expand All @@ -20,4 +21,5 @@ def custom_exception_handler(exc, context):
else:
resp.data = dict(code=resp.status_code, details=resp.data)
resp.data.setdefault('message', resp.status_text)
resp = JsonResponse(data=resp.data, status=resp.status_code)
return resp
4 changes: 2 additions & 2 deletions dogesec_commons/utils/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

HTTP404_EXAMPLE = OpenApiExample("http-404", {"message": "resource not found", "code": 404})
HTTP400_EXAMPLE = OpenApiExample("http-400", {"message": "request not understood", "code": 400})
WEBSERVER_404_RESPONSE = OpenApiResponse({'type': 'string', 'format': 'html', 'description': 'default 404 page'}, "webserver's HTML 404 page", examples=[OpenApiExample('404-page', "<html><title>page not found</title></html>")])
WEBSERVER_500_RESPONSE = OpenApiResponse({'type': 'string', 'format': 'html', 'description': 'default 500 page'}, "webserver's HTML 500 page", examples=[OpenApiExample('500-page', "<html><title>server error</title></html>")])

WEBSERVER_404_RESPONSE = OpenApiResponse(CommonErrorSerializer, description="webserver's HTML 404 page", examples=[OpenApiExample('404-page', {"code": 404, "message": "non-existent page"})])
WEBSERVER_500_RESPONSE = OpenApiResponse(CommonErrorSerializer, description="webserver's HTML 500 page", examples=[OpenApiExample('500-page', {"code": 500, "message": "internal server error"})])


DEFAULT_400_RESPONSE = OpenApiResponse(
Expand Down

0 comments on commit e984496

Please sign in to comment.