3

I'm trying to put an item and get it back a few statements later, but I'm getting a cryptic error from AWS.

class DataStore<Payload extends EventPayload> {   
    private final Clock clock;

    private final Table table;

    private final ObjectMapper objectMapper = new ObjectMapper();

    private final Class<Payload> payloadType;

    public DynamoDBEventStore(final Clock clock, final Class<Payload> eventClass, final String dataStoreName) {
        final AmazonDynamoDB client =
                AmazonDynamoDBClientBuilder.standard().build();

        final DynamoDB dynamoDB = new DynamoDB(client);

        this.table = dynamoDB.getTable(dataStoreName);
        this.clock = clock;
        this.payloadType = eventClass;
    }

    public void persist(final UUID eventId, final String aggregateId, final Long version, final Payload eventPayload) {
        final Map<String, Object> rawDomainEvent = Map.of(
                "EventId", eventId.toString(),
                "Timestamp", LocalDateTime.now(clock).toString(),
                "AggregateId", eventPayload.getAggregateKey(),
                "Version", version,
                "Payload", objectMapper.convertValue(eventPayload, Map.class)
        );
    
        final Item domainEvent = Item.fromMap(rawDomainEvent);
        table.putItem(domainEvent);
     }
    
        
    public void testEvent(final UUID eventId) {
        table.getItem("EventId", eventId.toString();
    }
}

If I save an item calling to persist, then the Item is saved as expected (see the JSON below from DynamoDB console).

 {
      "EventId": {
        "S": "8a2c1733-887d-42e1-b720-2e87dcf46269"
      },
      "Timestamp": {
        "S": "2021-10-10T04:18:56.465223700"
      },
      "Payload": {
        "M": {
          "transactions": {
            "L": []
          },
          "aggregateKey": {
            "S": "bc406432-37f1-440f-9157-b0ce8da814c1"
          }
        }
      },
      "Version": {
        "N": "1"
      },
      "AggregateId": {
        "S": "bc406432-37f1-440f-9157-b0ce8da814c1"
      }
    }

But when I call to testEvent it fails returning the following error:

  "message": "Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: E6M3NI8CTF05ONUC5G25H53PT7VV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)",

Any thoughts about what could be wrong with my code? FYI, I'm using Java 11, Spring Boot 2.5.4 and AWS SDK 1.12.81:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.12.81</version>
</dependency>
2
  • My guess is that you probably have a sort key defined in the database which you're not passing through to getItem. Do you have any sort keys defined? Commented Oct 10, 2021 at 21:10
  • Wrap table.getItem("EventId", eventId.toString(); in a try catch block please and let us know the details of the caught exception Commented Oct 10, 2021 at 21:12

1 Answer 1

3

The error message isn't that cryptic! Ignore the unmarshalling part, and you are left with

Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; 

I would guess, your eventId can't be found in the database - but you need to debug from here

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, if I put a non-existent EventId, it returns 404 but in this case, is just 400, so I don't understand why. If I go to the console, the EventId I'm looking for does exist :/
@TomásJuárez - good point; I guess I was focused on the "cryptic" nature of unmarshalling part :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.