Skip to content

Commit

Permalink
feat: handle and return errors as json in api
Browse files Browse the repository at this point in the history
  • Loading branch information
janezicmatej committed Nov 11, 2024
1 parent 6e5c7da commit b06b4ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
28 changes: 21 additions & 7 deletions fdc/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from drf_spectacular.utils import extend_schema
from rest_framework import decorators, response, viewsets
from drf_spectacular.utils import extend_schema, inline_serializer
from rest_framework import decorators, response, serializers, status, viewsets

from fdc.models import AttestationResult

Expand Down Expand Up @@ -35,7 +35,15 @@ def list(self, request, *args, **kwargs):

@extend_schema(
request=AttestationTypeGetByRoundIdBytesRequest,
responses=AttestationMinimalProofSerializer,
responses={
200: AttestationMinimalProofSerializer,
400: inline_serializer(
"AttestationTypeGetByRoundIdBytesErrorSerializer",
fields={
"error": serializers.CharField(),
},
),
},
)
@decorators.action(
detail=False, methods=["post"], url_path="get-proof-round-id-bytes"
Expand All @@ -47,10 +55,16 @@ def get_proof_round_id_bytes(self, request, *args, **kwargs):
_body.is_valid(raise_exception=True)
body = _body.validated_data

obj = self.get_queryset().get(
voting_round_id=body["votingRoundId"],
request_hex=body["requestBytes"],
)
try:
obj = self.get_queryset().get(
voting_round_id=body["votingRoundId"],
request_hex=body["requestBytes"],
)
except AttestationResult.DoesNotExist:
return response.Response(
data={"error": "attestation request not found"},
status=status.HTTP_400_BAD_REQUEST,
)

serializer = self.get_serializer(obj)

Expand Down
19 changes: 15 additions & 4 deletions ftso/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging

from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import extend_schema, inline_serializer
from py_flare_common.merkle import MerkleTree
from rest_framework import decorators, response, status, viewsets
from rest_framework import decorators, response, serializers, status, viewsets

from ftso.models import FeedResult, RandomResult
from ftso.serializers.data import (
Expand Down Expand Up @@ -55,7 +55,15 @@ def anchor_feed_names(self, request, *args, **kwargs):
@extend_schema(
parameters=[FeedResultFeedsWithProofsQuerySerializer],
request=FeedResultFeedsWithProofsRequestSerializer,
responses={200: MerkleProofValueSerializer(many=True)},
responses={
200: MerkleProofValueSerializer(many=True),
400: inline_serializer(
"AttestationTypeGetByRoundIdBytesErrorSerializer",
fields={
"error": serializers.CharField(),
},
),
},
)
@decorators.action(
detail=False, methods=["post"], url_path="anchor-feeds-with-proof"
Expand Down Expand Up @@ -87,7 +95,10 @@ def anchor_feeds_with_proof(self, request, *args, **kwargs):
.all()
)
if queryset is None:
return response.Response(None, status=status.HTTP_404_NOT_FOUND)
return response.Response(
data={"error": "anchor feeds not found"},
status=status.HTTP_400_BAD_REQUEST,
)

tree = get_merkle_tree_for_round(voting_round_id)
data = [
Expand Down

0 comments on commit b06b4ea

Please sign in to comment.