# Improve exception reporting for API
* fixes typo in exception_reporter.py * properly catches error on geometry cast into multipolygon if input are no valid polygons * extends error response on malicious api calls * specifies different exceptions on try-catch while initializing api data
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user