Skip to content

Instantly share code, notes, and snippets.

@Cibernomadas
Last active July 3, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cibernomadas/6c4204e2be585e782ab106a43156ae67 to your computer and use it in GitHub Desktop.
Save Cibernomadas/6c4204e2be585e782ab106a43156ae67 to your computer and use it in GitHub Desktop.
func RegisterRecovery() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
httprequest, _ := httputil.DumpRequest(c.Request, false)
pp.Println(err)
m := gomail.NewMessage()
m.SetHeader("From", "goblog@cibernomadas.es")
m.SetHeader("To", "admin@cibernomadas.es")
m.SetHeader("Subject", "GoBlog Error")
m.SetBody("text/plain", fmt.Sprintf(`Hi admin,\n unexpected error has happend en GoBlog.
Following there is the user request:\n%s
Following there is the panic error:\n\n%s`, string(httprequest), err))
d := gomail.Dialer{Host: "localhost", Port: 25}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
c.HTML(http.StatusOK, "index", gin.H{
"title": "Hi! GoBlog.",
"error": "You found an error. The administrator has been notified.",
})
}
}()
c.Next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment