[ci skip] updates on restructuring stuff

This commit is contained in:
Henrique Dias
2016-03-06 15:39:03 +00:00
parent b97ef80596
commit e33c79224f
9 changed files with 127 additions and 84 deletions

View File

@@ -1,6 +1,7 @@
package utils
import (
"encoding/json"
"errors"
"io"
"log"
@@ -236,3 +237,28 @@ func SplitCapitalize(name string) string {
return name
}
type jsonMSG struct {
Message string `json:"message"`
}
func RespondJSON(w http.ResponseWriter, message string, code int, err error) (int, error) {
msg := &jsonMSG{
Message: message,
}
m, err := json.Marshal(msg)
if err != nil {
return 500, err
}
if code == 500 && err == nil {
err = errors.New(message)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(m)
return 0, err
}