# Improve exception reporting for API #515

Merged
mpeltriaux merged 1 commits from improve_exception_reporting into master 2025-12-19 14:17:38 +01:00
5 changed files with 15 additions and 8 deletions
Showing only changes of commit 328f672ec0 - Show all commits

View File

@ -71,7 +71,7 @@ class APIV1CreateTestCase(BaseAPIV1TestCase):
# Expect this first request to fail, since user has no shared access on the intervention, we want to create
# a compensation for
response = self._run_create_request(url, post_body)
self.assertEqual(response.status_code, 500, msg=response.content)
self.assertEqual(response.status_code, 400, msg=response.content)
content = json.loads(response.content)
self.assertGreater(len(content.get("errors", [])), 0, msg=response.content)

View File

@ -6,7 +6,9 @@ Created on: 21.01.22
"""
import json
from json import JSONDecodeError
from django.core.exceptions import ObjectDoesNotExist
from django.http import JsonResponse, HttpRequest
from api.utils.serializer.v1.compensation import CompensationAPISerializerV1
@ -66,8 +68,12 @@ class AbstractAPIViewV1(AbstractAPIView):
body = request.body.decode("utf-8")
body = json.loads(body)
created_id = self.serializer.create_model_from_json(body, self.user)
except Exception as e:
return self._return_error_response(e, 500)
except (JSONDecodeError,
AssertionError,
ValueError,
PermissionError,
ObjectDoesNotExist) as e:
return self._return_error_response(e, 400)
return JsonResponse({"id": created_id})
def put(self, request: HttpRequest, id=None):

View File

@ -81,9 +81,7 @@ class AbstractAPIView(View):
Returns:
"""
content = [error.__str__()]
if hasattr(error, "messages"):
content = error.messages
content = [f"{error.__class__.__name__}: {str(error)}"]
return JsonResponse(
{
"errors": content

View File

@ -407,7 +407,10 @@ class Geometry(BaseResource):
"""
output_geom = input_geom
if not isinstance(input_geom, MultiPolygon):
try:
output_geom = MultiPolygon(input_geom, srid=DEFAULT_SRID_RLP)
except TypeError as e:
raise AssertionError(f"Only (Multi)Polygon allowed! Could not convert {input_geom.geom_type} to MultiPolygon")
return output_geom
@staticmethod

View File

@ -33,7 +33,7 @@ class KonovaExceptionReporter(ExceptionReporter):
"""
whitelist = [
"is_email",
"unicdoe_hint",
"unicode_hint",
"frames",
"request",
"user_str",