Skip to content

Commit

Permalink
JSON Parsing Errors: Make errors less verbose (#10891)
Browse files Browse the repository at this point in the history
* JSON Parsing Errors: Make errors less verbose

* Only intercept when JSON is invalid
  • Loading branch information
Maffooch authored Sep 16, 2024
1 parent df6c17d commit 885029c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion dojo/api_v2/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core.exceptions import ValidationError
from django.db.models.deletion import RestrictedError
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework.status import (
HTTP_400_BAD_REQUEST,
Expand All @@ -20,7 +21,11 @@ def custom_exception_handler(exc, context):
# to get the standard error response.
response = exception_handler(exc, context)

if isinstance(exc, RestrictedError):
if isinstance(exc, ParseError) and "JSON parse error" in str(exc):
response = Response()
response.status_code = HTTP_400_BAD_REQUEST
response.data = {"message": "JSON request content is malformed"}
elif isinstance(exc, RestrictedError):
# An object cannot be deleted because it has dependent objects.
response = Response()
response.status_code = HTTP_409_CONFLICT
Expand Down

0 comments on commit 885029c

Please sign in to comment.