recovery

package module
v0.0.0-...-a6bc1b3 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2014 License: MIT Imports: 8 Imported by: 0

README

JSON Recovery middleware for negroni

Martini is a framework for quickly writing modular web applications/services 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

NOTE: this middleware relies on martini.Env being set. In production mode, it will render a generic error message and exclude the line number for security reasons.

Usage

Installation

Installation is the same as any other package

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

Make sure you have imported the package:

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

Then add to the middleware stack:

m.Use(recovery.JSONRecovery())

Full Example

package main

import (
	"github.com/albrow/martini-json-recovery"
	"github.com/go-martini/martini"
)

func main() {
	m := martini.Classic()
	m.Use(recovery.JSONRecovery())
	m.Get("/", func() string {
		panic("Oh no! Something went wrong")
	})
	m.Run()
}

The above code will render the following response:

{
    "Code": 500,
    "Short": "internalError",
    "Errors": [
        "Oh no! Something went wrong"
    ],
    "From": "/Users/alex/programming/go/src/github.com/albrow/testing/martini-panic/server.go:12"
}

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONRecovery

func JSONRecovery() martini.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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