ex_aws_dynamo icon indicating copy to clipboard operation
ex_aws_dynamo copied to clipboard

Support condition failure return values

Open chriskdon opened this issue 1 year ago • 0 comments
trafficstars

This PR adds support for the :return_values_on_condition_check_failure option on the put_item, update_item, and delete_item operations. More specifically, it adds support for returning the old item in the error, which helps determine why a condition check failed. It allows you to compare the current database value with the condition to determine what condition failed.

The most controversial aspect of this change will likely be the addition of a new error structure for the ConditionalCheckFailedException. I've included examples below of what the errors look like for various cases. I've tried to make the changes work in such a way that if return_values_on_condition_check_failure was never specified (which it shouldn't be, as it's not in the type spec for those functions) the behaviour is unchanged. Only when return_values_on_condition_check_failure: :all_old is used does a user now need to worry about a new error format.

All that being said, I understand if you want to avoid changing the error format. However, I suggest at least considering the changes to the type specs and the option parsing for return_values_on_condition_check_failure. Those changes fix some dialyzer and parser errors when trying to specify the attribute. With these changes, users can still override the error_parser themselves if they need to get access to the item.

Example Error Cases

return_values_on_condition_check_failure: :none / default / (unchanged behaviour)

When there is an old item available:

{
  "ConditionalCheckFailedException",
  "The conditional request failed"
}

When there is no old item available:

{
  "ConditionalCheckFailedException",
  "The conditional request failed"
}

return_values_on_condition_check_failure: :all_old

When there is an old item available:

{
  "ConditionalCheckFailedException",
  "The conditional request failed",
  %{"admin" => %{"BOOL" => false}, "age" => %{"N" => "25"}, "email" => %{"S" => "[email protected]"}, "name" => %{"M" => %{"first" => %{"S" => "bob"}, "last" => %{"S" => "bubba"}}}}
}

When there is no old item available:

{
  "ConditionalCheckFailedException",
  "The conditional request failed"
}

I'm open to any and all other suggestions, so please feel free to leave comments or nitpicks. :)

chriskdon avatar Jun 25 '24 04:06 chriskdon