英文:
How can I catch 410 error from `postToConnection` call via golang aws sdk?
问题
我正在使用AWS的websocket apigateway来管理websocket连接。有时候连接会断开,但@disconnect
路由没有被触发。在这种情况下,postToConnection
API调用会返回410错误。
我正在使用golang的sdk github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi
。postToConnection
API返回PostToConnectionOutput, error
。我该如何判断错误是否为410错误?
英文:
I am using websocket apigateway in AWS to manage websocket connections. There are cases that the connection is disconnected but the @disconnect
route is not triggered. In this case, postToConnection
api call will return 410 error.
I am using golang sdk github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi
. The postToConnection
API returns PostToConnectionOutput, error
. How can I detect whether the error is 410 error?
答案1
得分: 3
你可以检查返回的错误是否是GoneException
类型。
请参阅https://aws.github.io/aws-sdk-go-v2/docs/handling-errors/
import(
api "github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
apitypes "github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi/types"
)
func main() {
svc := api.New(api.Options{
Region: "your-region",
})
output, err := svc.PostToConnection(ctx, &api.PostToConnectionInput{
ConnectionId: aws.String("your-connection-id"),
})
if err != nil {
var gne *apitypes.GoneException
if errors.As(err, &gne) {
log.Println("error:", gne.Error())
}
return
}
}
英文:
You can check the returned error is a type of GoneException
See https://aws.github.io/aws-sdk-go-v2/docs/handling-errors/
import(
api "github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
apitypes "github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi/types"
)
func main() {
svc := api.New(api.Options{
Region: "your-region",
})
output, err := svc.PostToConnection(ctx, &api.PostToConnectionInput{
ConnectionId: aws.String("your-connection-id"),
})
if err != nil {
var gne *apitypes.GoneException
if errors.As(err, &gne) {
log.Println("error:", gne.Error())
}
return
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论