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?

王林
Release: 2024-02-06 08:30:08
forward
1041 people have browsed it

如何通过 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
        }
    }
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template