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 fromstatusCode
.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.
go get github.com/darahayes/go-boom
import (
"github.com/darahayes/go-boom"
)
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 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:
- Recovers from unexpected panics.
- Logs a stack trace in the server logs.
- Uses
boom.Internal()
to return a generic500 Internal Server Error
. This prevents accidental leakage of sensitive info in the response.
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)
}
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
}
To see the full list of API methods check out the documentation on godoc.org