💥 Go HTTP middleware that catches any panics and serves a proper error response.
Go
Latest commit 1c296bb Sep 30, 2015 @dre1080 Update README.
Permalink
Failed to load latest commit information.
examples Initial commit. Sep 29, 2015
img Initial commit. Sep 29, 2015
LICENSE Initial commit. Sep 29, 2015
README.md Update README. Sep 30, 2015
doc.go Initial commit. Sep 29, 2015
recover.go Initial commit. Sep 29, 2015
template.go Fix issue with highlight in template. Sep 30, 2015
util.go Initial commit. Sep 29, 2015

README.md

Recover GoDoc License

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

Terminal

HTML

HTML

JSON

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()
}