DynamoDB-错误为ResourceNotFoundException,但在Go中强制转换失败



我写了一些代码,试图使用AWS SDK V2 DynamoDB包在Go中获得表格描述:

// 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
}

在测试过程中,此代码返回以下错误:

操作错误DynamoDB:DescriptionTable,https响应错误StatusCode:400,RequestID:4b0bcb2b-c833-459f-9db2-544841aabbd3,ResourceNotFoundException

我遇到的问题是,这显然是一个ResourceNotFoundException,但转换不起作用。我还有什么需要做的吗?

我找到了一个解决方案。首先,我遇到的一个问题是,我正在导入github.com/aws/aws-sdk-go-v2/service/sso/types而不是github.com/aws/aws-sdk-go-v2/service/dynamodb/types,所以我的演员阵容永远不会工作。话虽如此,做出这一改变并没有解决问题。

在日志调试之后,我发现v2 SDK将AWS错误封装在*smithy.OperationError类型中。因此,直接铸造和errors.Is不起作用。

我在这里所做的实际工作是将我的错误检查代码更改为:

if temp := new(types.ResourceNotFoundException); !errors.As(err, &temp) {
return err
} else {
create = true
}

最新更新