This repository has been archived by the owner. It is now read-only.
💥 Go HTTP middleware that catches any panics and serves a proper error response.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
img
LICENSE
README.md
doc.go
recover.go
template.go
util.go

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