Giter VIP home page Giter VIP logo

go-boom's Introduction

Boom

Documentation Coverage Status Go Report Card CircleCI

boom provides a set of functions for returning HTTP errors. Each function responds with a JSON object which includes the following properties:

  • statusCode - the HTTP status code.
  • error- the HTTP status message (e.g. 'Bad Request', 'Internal Server Error') derived from statusCode.
  • message - the error message which can be optionally set by the developer.

All boom functions take a http.ResponseWriter as an argument which means boom should be compatible with any Golang http frameworks that also use http.ResponseWriter.

To see the full list of boom functions, check out the documentation on godoc.org

This library is inspired by the wonderful JavaScript library https://www.npmjs.com/package/boom.

Install

go get github.com/darahayes/go-boom

Import

import (
	"github.com/darahayes/go-boom"
)

Example Usage

package main

import (
	"fmt"
	"net/http"
	"github.com/darahayes/go-boom"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
	boom.NotFound(w, "Sorry, there's nothing here.")
}

func main() {
	http.HandleFunc("/", myHandler)
	http.ListenAndServe(":8080", nil)
}

With this example, the response from the / endpoint would be:

{
  "error": "Not Found",
  "message": "Sorry, there's nothing here.",
  "statusCode": 404
}

boom also accepts arguments of type error, or any struct that implements the built in error interface. (i.e. has an Error() function which returns a string)

func myHandler(w http.ResponseWriter, r *http.Request) {
	err := errors.New("You shall not pass!")
	boom.Unathorized(w, err)
}

and the response:

{
  "error": "Unathorized",
  "message": "You shall not pass!",
  "statusCode": 401
}

It's also possible to provide no error message at all:

func myHandler(w http.ResponseWriter, r *http.Request) {
	boom.BadRequest(w)
}

and the response:

{
  "error": "Bad Request",
  "message": "Bad Request",
  "statusCode": 400
}

boom.RecoverHandler

boom also comes with a RecoverHandler middleware function that can be used to recover from unexpected panics. It can be used directly or with any router library that deals with the http.Handler type. (most of them do!)

boom.RecoverHandler does three things:

  1. Recovers from unexpected panics.
  2. Logs a stack trace in the server logs.
  3. Uses boom.Internal() to return a generic 500 Internal Server Error. This prevents accidental leakage of sensitive info in the response.

Example usage with plain http library

func myHandler(w http.ResponseWriter, r *http.Request) {
	panic("Uh oh, something happened")
}

func main() {
	http.Handle("/", boom.RecoverHandler(http.HandlerFunc(myHandler)))
	http.ListenAndServe(":8080", nil)
}

Example usage with the mux router

func myHandler(w http.ResponseWriter, r *http.Request) {
	panic("Uh oh, something happened")
}

func main() {
	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/", myHandler)
	
	router.Use(boom.RecoverHandler)
	
	http.ListenAndServe(":8080", router)
}

In both examples above, a stack trace is printed in the server logs and the response is the following:

{
  "error": "Internal Server Error",
  "message": "Internal Server Error",
  "statusCode": 500
}

API Methods

To see the full list of API methods check out the documentation on godoc.org

go-boom's People

Contributors

carloserocha avatar darahayes 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.