I wrote some code to attempt to get a table description in Go, using the AWS SDK V2 DynamoDB package:
// First, create a connection to our local DynamoDB
client := dynamodb.NewFromConfig(cfg)
// Next, attempt to get the table description associated with the table name
output, err := client.DescribeTable(ctx, &dynamodb.DescribeTableInput{
TableName: table.TableName,
})
// Now, if we got an error then check if it was a resource-not-found exception. If
// it was then that means we should create the table; otherwise, it means that something
// isn't right so return it. If the description was nil, we'll also create the table
var create bool
if err != nil {
if _, ok := err.(*types.ResourceNotFoundException); !ok {
return err
} else {
create = true
}
} else if output == nil {
create = true
}
During testing, this code returned the following error:
operation error DynamoDB: DescribeTable, https response error StatusCode: 400, RequestID: 4b0bcb2b-c833-459f-9db2-54841aa1bbd3, ResourceNotFoundException
The problem I'm having is that this is clearly a ResourceNotFoundException
but the cast is not working. Is there something else I need to do to get this to work?
CodePudding user response:
I have found a solution. First, one of the issues I had was that I was importing github.com/aws/aws-sdk-go-v2/service/sso/types
instead of github.com/aws/aws-sdk-go-v2/service/dynamodb/types
so my cast would never have worked. That being said, making this change did not fix the issue.
After log-debugging, I discovered that the v2 SDK wraps the AWS errors in a *smithy.OperationError
type. Therefore, direct-casting and errors.Is
won't work.
What I did that actually worked here was to change my error checking code to this:
if temp := new(types.ResourceNotFoundException); !errors.As(err, &temp) {
return err
} else {
create = true
}