DynamoDB – 错误是 ResourceNotFoundException,但在 Go 中转换失败。

huangapple go评论77阅读模式
英文:

DynamoDB - Error is ResourceNotFoundException but cast fails in Go

问题

我写了一些代码,尝试使用AWS SDK V2 DynamoDB package在Go中获取表描述:

// 首先,创建到本地DynamoDB的连接
client := dynamodb.NewFromConfig(cfg)

// 接下来,尝试获取与表名相关联的表描述
output, err := client.DescribeTable(ctx, &dynamodb.DescribeTableInput{
	TableName: table.TableName,
})

// 现在,如果出现错误,则检查是否为资源未找到异常。如果是,则表示应该创建表;否则,表示有问题,返回错误。如果描述为nil,我们也将创建表
var create bool
if err != nil {
	if _, ok := err.(*types.ResourceNotFoundException); !ok {
		return err
	} else {
		create = true
	}
} else if output == nil {
	create = true
}

在测试期间,这段代码返回了以下错误:

> 操作错误 DynamoDB: DescribeTable,https响应错误 StatusCode: 400,RequestID: 4b0bcb2b-c833-459f-9db2-54841aa1bbd3,ResourceNotFoundException

我遇到的问题是,明显是ResourceNotFoundException,但类型转换不起作用。我需要做其他什么来使其工作吗?

英文:

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?

答案1

得分: 1

我找到了一个解决方案。首先,我遇到的一个问题是我导入了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
}
英文:

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
}

huangapple
  • 本文由 发表于 2022年9月12日 14:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73685152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定