Recover
Recover is a HTTP middleware that catches any panics and serves a proper error response.
Works with all frameworks that support native http handler (eg. Echo, Goji).
Installation
$ go get github.com/dre1080/recover
Usage
package main
import (
"log"
"net/http"
"github.com/dre1080/recover"
)
var myPanicHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("you should not have a handler that just panics ;)")
})
func main() {
recovery := recover.New(&recover.Options{
Log: log.Print,
})
// recoveryWithDefaults := recovery.New(nil)
app := recovery(myPanicHandler)
http.ListenAndServe("0.0.0.0:3000", app)
}
Terminal
HTML
JSON
Echo Example
...
func main() {
e := echo.New()
e.Use(recover.New(&recover.Options{
Log: log.Print,
}))
...
e.Run(":3000")
}
Goji Example
...
func main() {
goji.Use(recover.New(&recover.Options{
Log: log.Print,
}))
...
goji.Serve()
}