CustomRecovery not working
ping2ravi opened this issue · comments
Ravi Sharma commented
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.