Package junkd0g/neji is a simple JSON error response library.
go get -u github.com/junkd0g/neji
go test ./...
package main
import (
"errors"
"net/http"
"github.com/gorilla/mux"
nerror "github.com/junkd0g/neji"
)
func HelloWorld(w http.ResponseWriter, r *http.Request) {
err := errors.New("This is the best error message ever")
errorResponse, _ := nerror.SimpeErrorResponseWithStatus(500, err)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(errorResponse))
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", HelloWorld)
http.ListenAndServe(":8076", router)
}
nerror.SimpeErrorResponseWithStatus(500, err)
/*
will return this response
{
"message" : "Your json is wrong or something",
"status" : 500
}
*/
nerror.SimpeErrorResponseWithStatusV2(500, err)
/*
will return this response
{
"error" : {
"status" : 502,
"message" : "Bad gateway."
}
}
*/
- Iordanis Paschalidis -junkd0g