skip to Main Content

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?

2

Answers


  1. 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
            }
        }
    
    Login or Signup to reply.
  2. To detect whether the error returned from the postToConnection API is a 410 error, you can check the error type using a type assertion.

    Here is an example:

    import "github.com/aws/aws-sdk-go-v2/service/apigatewaymanagementapi"
    
    func myFunction() {
        // Create a new client
        client := apigatewaymanagementapi.New(myConfig)
    
        // Make the postToConnection API call
        _, err := client.PostToConnection(myContext, myInput)
    
        // Check if the error is a 410 error
        if aerr, ok := err.(apigatewaymanagementapi.GoneException); ok {
            // Handle the 410 error here
            // e.g. reconnect or close the connection
        } else {
            // Handle other errors here
            // e.g. log the error or retry the API call
        }
    }
    

    In the above example, we first make the postToConnection API call and check the error returned by the API call. We then use a type assertion to check if the error is of type apigatewaymanagementapi.GoneException. If the error is a GoneException, we can handle it appropriately, for example, by reconnecting or closing the connection.

    If the error is not a GoneException, we can handle it in some other way, for example, by logging the error or retrying the API call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search