GithubHelp home page GithubHelp logo

soroushjp / negroni-json-recovery Goto Github PK

View Code? Open in Web Editor NEW

This project forked from albrow/negroni-json-recovery

0.0 3.0 0.0 115 KB

Middleware for negroni which catches panics and wraps them into a JSON response.

License: Other

Go 100.00%

negroni-json-recovery's Introduction

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": [
        "Oh no! Something went wrong in HandleFunc"
    ],
    "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.

negroni-json-recovery's People

Contributors

albrow avatar soroushjp avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.