NOT ANYMORE
This really basic JSend implementation does so much for you. The package basically just exports one Function. It is that easy
The usual way go get github.com/klyngen/jsend
This is how simple we can make the response...
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
jsend.FormatResponse(w, "Time to read the documentation?", jsend.NotFound)
}
Fisty comments in the message body is optional
Mux is a great router. What is really nice, is that mux allows us to replace its NotFound-handler and MethodNotAllowed-handler; like so:
router.MethodNotAllowedHandler = http.HandlerFunc(methodNotAllowedHandler)
By using this library, you don't need to pay attention to the structure of the Jsend-formatted message. The library does that for you.
{
"status": "client-error",
"message": "We do not support that method for this endpoint"
}
Since the data was a string and the type was an error, we know that data should not be present.
Not-mandatory fields are omitted, as they should
Becauce I like it that way, the status-object can be: success
, server-error
and client-error
.