github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/httputil/write_response.go (about) 1 package httputil 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/sirupsen/logrus" 8 ) 9 10 func WriteResponse(w http.ResponseWriter, code int, object interface{}) { 11 data, err := json.Marshal(object) 12 if err != nil { 13 w.WriteHeader(http.StatusInternalServerError) 14 return 15 } 16 17 w.Header().Set("Content-Type", "application/json") 18 w.WriteHeader(code) 19 _, err = w.Write(data) 20 if err != nil { 21 logrus.Warnf("could not write response %s", string(data)) 22 } 23 } 24 25 type errObj struct { 26 Error string `json:"error"` 27 } 28 29 func WriteErrorResponse(w http.ResponseWriter, code int, err error) { 30 WriteResponse(w, code, errObj{Error: err.Error()}) 31 }