Skip to content

Instantly share code, notes, and snippets.

@alperen
Last active February 19, 2020 12:14
Show Gist options
  • Save alperen/c064444cb3d2a89c7f7a3eda91f8eedf to your computer and use it in GitHub Desktop.
Save alperen/c064444cb3d2a89c7f7a3eda91f8eedf to your computer and use it in GitHub Desktop.
Go - Gin Framework Handle Panics
func handlePanics() gin.HandlerFunc {
return func(context *gin.Context) {
defer func() {
if rval := recover(); rval != nil {
context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{error: rval.(string)})
// do more
return
}
}()
context.Next()
}
}
func main() {
//....
r.Use(handlePanics())
//....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment