This commit is contained in:
Jeffrey Duroyon
2019-04-04 14:29:48 +02:00
committed by Jeffrey Duroyon
parent 0df6d64c35
commit 33db360b03
38 changed files with 1476 additions and 1 deletions

31
internal/utils/responses.go Executable file
View File

@@ -0,0 +1,31 @@
package utils
import (
"encoding/json"
"github.com/kratisto/mam-contract/internal/storage/model"
"net/http"
)
func JSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set(HeaderNameContentType, HeaderValueApplicationJSONUTF8)
w.WriteHeader(status)
if data != nil {
json.NewEncoder(w).Encode(data)
}
}
func JSONError(w http.ResponseWriter, e model.APIError) {
if e.Headers != nil {
for k, headers := range e.Headers {
for _, headerValue := range headers {
w.Header().Add(k, headerValue)
}
}
}
JSON(w, e.HTTPCode, e)
}
func JSONErrorWithMessage(w http.ResponseWriter, e model.APIError, message string) {
e.Description = message
JSONError(w, e)
}