New srverrors package added. New error handling added to control package

This commit is contained in:
Héctor Hurtado
2019-11-18 14:32:41 +01:00
parent 2c32daedcb
commit edff842c3b
4 changed files with 128 additions and 42 deletions
+19
View File
@@ -0,0 +1,19 @@
package srverrors
import (
"encoding/json"
"net/http"
)
type ServerErrMessage struct {
Reason string
}
func WriteErrorResponse(statusCode int, reasonMsg string, res http.ResponseWriter) {
respBody := ServerErrMessage{}
respBody.Reason = reasonMsg
bb, _ := json.Marshal(respBody)
res.Header().Add("Content-Type", "application/json; charset=utf-8")
res.WriteHeader(statusCode)
_, _ = res.Write(bb)
}