Home >Backend Development >Golang >How to catch 410 error from `postToConnection` call via golang aws sdk?

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

王林
王林forward
2024-02-06 08:30:081037browse

如何通过 golang aws sdk 从 `postToConnection` 调用捕获 410 错误?

Question content

I am using websocket apigateway in AWS to manage websocket connections. There are situations where the connection is disconnected but the @disconnect route is not triggered. In this case, the postToConnection api call will return a 410 error.

I am using golang sdk github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi. postToConnection API returned PostToConnectionOutput with error . How to detect if an error is a 410 error?


Correct answer


You can check that the returned error is goneexceptiontype

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

The above is the detailed content of How to catch 410 error from `postToConnection` call via golang aws sdk?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete