recover

package module
v0.0.0-...-1c296bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2015 License: MIT Imports: 12 Imported by: 0

README

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

Documentation

Overview

Package recover is a HTTP middleware that catches any panics and serves a proper error response.

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

A GET request to "/" will output:

[PANIC] you should not have a handler that just panics ;)

Stack Trace:
============

path/to/main.go:11                                              glob.func1
/usr/local/Cellar/go/1.5.1/libexec/src/net/http/server.go:1423  HandlerFunc.ServeHTTP
github.com/dre1080/recover/recover.go:66                        New.func1.1
/usr/local/Cellar/go/1.5.1/libexec/src/net/http/server.go:1423  HandlerFunc.ServeHTTP
/usr/local/Cellar/go/1.5.1/libexec/src/net/http/server.go:1863  serverHandler.ServeHTTP
/usr/local/Cellar/go/1.5.1/libexec/src/net/http/server.go:1362  (*conn).serve
/usr/local/Cellar/go/1.5.1/libexec/src/runtime/asm_amd64.s:1697 goexit

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts *Options) func(h http.Handler) http.Handler

New returns a middleware that: recovers from panics, logs the panic and backtrace, writes a HTML response and Internal Server Error status.

If a JSON content type is detected it returns a JSON response.

Types

type Options

type Options struct {
	Log func(v ...interface{})
}

Options for the recover middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL