awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.03k stars 198 forks source link

CustomRecovery not working #129

Open ping2ravi opened 2 years ago

ping2ravi commented 2 years ago

Hi , I am trying to create a global error handler using gin's CustomRecovery. From documentation it looks straightforward but its not working as expected.

gin Initialization

//Using New instead of Default to make sure default recovery and Logger dont get used
        r := gin.New()
    r.Use(DummyMiddleware)
    r.GET("/fail", FailHandler)

    r.Use(gin.CustomRecovery(MyCustomRecovery))
    ginLambda = ginadapter.NewV2(r)

My CustomRecovery is

func MyCustomRecovery(c *gin.Context, recovered interface{}) {
    log.Printf("Error Caught, Recovering now")
    if err, ok := recovered.(string); ok {
        log.Printf("Found Error string %s", err)
        // c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
        c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": false, "message": err})
    }
    c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"status": false, "message": "Unknown Error"})
}

Fail Handler

func FailHandler(c *gin.Context) {
    log.Printf("Failing it now")
    panic("Forced Failure")
}

Handler

func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
    awsResponse, err := ginLambda.ProxyWithContext(ctx, request)
    return awsResponse, err
}

main

func main() {
    lambda.Start(Handler)
}

I build it and deploy to AWS, but when i call /fail url, MyCustomRecovery method never gets called and I get 502 Error without any body.

Not sure what i am doing wrong or its a bug. Any help will be appreciated, already spent 2 days looking into it.