-
Notifications
You must be signed in to change notification settings - Fork 655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DynamoDB/AWS SDK error handling doesn't work properly #1773
Comments
Hi, Thanks for opening the ticket. |
Hi @yerke , As I mentioned on the separate thread you referenced. This is not a known error with the DynamoDB API. I am not able to reproduce your issue. Since your code is missing a lot of things like what your Here is my complete code package main
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type Office struct {
Season int
Episode int
Foo string
}
func main() {
cfg, err := config.LoadDefaultConfig(
context.Background(),
config.WithRegion("us-east-1"),
)
if err != nil {
panic(fmt.Sprintf("Error configuring AWS: %s", err))
}
db := dynamodb.NewFromConfig(cfg)
item, err := attributevalue.MarshalMap(&Office{
Season: 11,
Episode: 11,
Foo: "bar",
})
if err != nil {
fmt.Println(err)
}
putItemInput := dynamodb.PutItemInput{
TableName: aws.String("TEST_TABLE"),
Item: item,
ConditionExpression: aws.String("attribute_not_exists(#Foo)"),
ExpressionAttributeNames: map[string]string{
"#Foo": "Foo",
},
}
putItemOutput, err := db.PutItem(context.Background(), &putItemInput)
if err != nil {
var ccf *types.ConditionalCheckFailedException
if errors.As(err, &ccf) {
fmt.Println("this is a smithy generated error")
}
return
}
fmt.Printf("%v\n", putItemOutput)
} The first time the code ran it created the record in DDB, after I changed the value of Foo and executed the file again the output was as expected: This is a smithy generated error
Let me know if this helps. |
This issue has not received a response in 1 week. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled. |
@RanVaknin I will try to make a smaller repro for you. |
This issue has not received a response in 1 week. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled. |
Commenting for any future readers. What tripped me up was "double pointer" thing as @RanVaknin showed. // Doing an update. If Key doesn't exist, just add a new item. Return nothing.
_, err = d.Client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
Key: map[string]types.AttributeValue{
"userId": &types.AttributeValueMemberN{Value: strconv.Itoa(userId)},
},
TableName: aws.String("myTableName"),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
UpdateExpression: expr.Update(),
ReturnValues: types.ReturnValueNone,
})
if err != nil {
var opErr *smithy.OperationError
if errors.As(err, &opErr) {
switch opErr.Unwrap().(type) {
case *types.ProvisionedThroughputExceededException:
// handle update rate too high
case *types.RequestLimitExceeded:
// handle ran out of money
default:
// handle some other operational error
}
}
return fmt.Errorf("unexpected error: %w", err)
}
return nil |
no, above example would not work for ConditionalCheckFailedException casting. |
Describe the bug
We are calling DynamoDB like follows:
Then we are trying to handle exception
This way of handling exception in aws-sdk-go-v2 is suggested in migration guide: https://aws.github.io/aws-sdk-go-v2/docs/handling-errors/#api-error-responses
Expected Behavior
I expect that if DynamoDB returns
ConditionalCheckFailedException
,errors.As
call as written above to return true.Current Behavior
The problem is that it doesn't work. I can see that that err contains
*smithy.OperationError: operation error DynamoDB: PutItem, https response error StatusCode: 400, RequestID: UVDSN7JDGHJFC7AGUH3UNQQ4IFVV4KQNSO5AEMVJF66Q9ASUAAJG, ConditionalCheckFailedException: The conditional request failed
, buterrors.As(err, &ccf)
does not return true.Reproduction Steps
Please see code above
Possible Solution
I had to use a workaround as suggested in https://github.com/aws/aws-sdk-go-v2/issues/1386.
Additional Information/Context
It looks similar to https://github.com/aws/aws-sdk-go-v2/issues/1386.
I have a support case open with Case ID 10424576731.
AWS Go SDK V2 Module Versions Used
Compiler and Version used
go1.17.6
Operating System and version
Linux, CentOS 7
The text was updated successfully, but these errors were encountered: