recovery

package module
v0.0.0-...-7047e5b Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2015 License: MIT Imports: 8 Imported by: 4

README

JSON Recovery middleware for negroni

Negroni is an idiomatic approach to web middleware in Go.

This recovery middleware catches any panics and wraps them up into a json response, including the line number where the panic occured. Borrows heavily from the default recovery middleware in martini.

See also https://github.com/go-martini/martini/blob/master/LICENSE

Usage

Installation

Installation is the same as any other package

go get github.com/albrow/negroni-json-recovery

Make sure you have imported the package:

import "github.com/albrow/negroni-json-recovery"

Then add to the middleware stack:

n.Use(recovery.JSONRecovery(true))

In production, you should set fullMessages to 'false' to show a generic error message:

n.Use(recovery.JSONRecovery(false))

Full Example

package main

import (
	"github.com/albrow/negroni-json-recovery"
	"github.com/codegangsta/negroni"
	"net/http"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		panic("Oh no! Something went wrong in HandleFunc")
	})

	n := negroni.New(negroni.NewLogger())
	n.Use(recovery.JSONRecovery(true))
	n.UseHandler(mux)
	n.Run(":3000")
}

With fullMessages set to 'true', the above code will render the following response:

{
    "Code": 500,
    "Short": "internalError",
    "Errors": [
        "dial tcp 127.0.0.1:6379: connection refused"
    ],
    "From": "/Users/alex/programming/go/src/github.com/albrow/testing/negroni-panic/server.go:12"
}

or with fullMessages set to 'false', it will render:

{
    "Code": 500,
    "Short": "internalError",
    "Errors": [
        "Something went wrong :("
    ]
}

You can also inspect console for a more detailed message and full stack trace.

Custom Response Formats

You can change the response format by overriding the Formatter function. Formatter is a function which accepts a variety of parameters related to the error that occurred and returns an empty interface. The interface will be converted to JSON and rendered in the response.

Here's an example of a custom formatter which only spits out the error message and no other information.

recovery.Formatter = func(errMsg string, stack []byte, file string, line int, fullMessages bool) interface{} {
	return map[string]string{
		"error": errMsg,
	}
}

With this custom formatter, the rendered message would look like this:

{
	"error": "dial tcp 127.0.0.1:6379: connection refused"
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFormatter = func(errMsg string, stack []byte, file string, line int, fullMessages bool) interface{} {
	return newJSONPanicError(errMsg, fullMessages, file, line)
}

DefaultFormatter returns a jsonPanicError constructed from the parameters. jsonPanicError contains 4 fields: - Code: the http status code (always 500) - Short: a short message specifying what error occured (always "internalError") - Errors: an array of error messages - From: the file and line number where the error originated

View Source
var Formatter func(errMsg string, stack []byte, file string, line int, fullMessages bool) interface{} = DefaultFormatter

Formatter lets you specify any arbitrary interface based on the function parameters which will be converted to JSON and written to the response.

View Source
var IndentJSON = false

IndentJSON specifies whether or not to indent (or pretty print) the json returned when there is a panic. The default is false.

View Source
var StackDepth = 2

StackDepth lets you specify how many stack frames to skip before reaching the file and line number you care about. You probably want to set this so that recovery skips frames in the stack until it reaches the line where panic was called. This can depend on how you included the middleware and recovered from panics. The default is 2

Functions

func JSONRecovery

func JSONRecovery(fullMessages bool) negroni.HandlerFunc

JSONRecovery returns a middleware function which catches any panics and wraps them up into a JSON response. Set fullMessages to true if you would like to show full error messages with line numbers and false if you would like to protect this information. Typically, in development mode you would set fullMessages to true, but in production you would set it to false.

Types

This section is empty.

Jump to

Keyboard shortcuts

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