Commit a2097c18 authored by Janos Guljas's avatar Janos Guljas

export jsonhttp StatusResponse

parent baada7f6
......@@ -22,21 +22,24 @@ var (
EscapeHTML = false
)
// Respond writes a JSON-encoded body to http.ResponseWriter.
func Respond(w http.ResponseWriter, statusCode int, response interface{}) {
type statusResponse struct {
// StatusResponse is a standardized error format for specific HTTP responses.
// Code field corresponds with HTTP status code, and Message field is a short
// description of that code or provides more context about the reason for such
// response.
type StatusResponse struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
}
// Respond writes a JSON-encoded body to http.ResponseWriter.
func Respond(w http.ResponseWriter, statusCode int, response interface{}) {
if response == nil {
response = &statusResponse{
response = &StatusResponse{
Code: statusCode,
Message: http.StatusText(statusCode),
}
} else if message, ok := response.(string); ok {
response = &statusResponse{
response = &StatusResponse{
Code: statusCode,
Message: message,
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment