登录
首页 >  Golang >  Go问答

Go 中 DynamoDB 转换失败的错误 ResourceNotFoundException

来源:stackoverflow

时间:2024-02-06 15:42:22 270浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Go 中 DynamoDB 转换失败的错误 ResourceNotFoundException》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我编写了一些代码来尝试使用 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:describetable,https 响应错误 statuscode:400,requestid:4b0bcb2b-c833-459f-9db2-54841aa1bbd3,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
}

理论要掌握,实操不能落!以上关于《Go 中 DynamoDB 转换失败的错误 ResourceNotFoundException》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>