How can I catch 410 error from `postToConnection` call via golang aws sdk?

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

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

huangapple
  • 本文由 发表于 2023年5月3日 16:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76161747.html
匿名

发表评论

匿名网友

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

确定