diff --git a/status.go b/status.go index 0bf40bc..769de43 100644 --- a/status.go +++ b/status.go @@ -1,7 +1,28 @@ package networkutil -import "net/http" +import ( + "encoding/json" + "net/http" +) + +type errorResponse struct { + StatusCode int `json:"status_code"` + Status string `json:"status"` +} func ErrorStatus(w http.ResponseWriter) { w.WriteHeader(http.StatusBadRequest) } + +func ErrorResponse(w http.ResponseWriter, statusCode int, err error) { + response := errorResponse{ + StatusCode: statusCode, + Status: err.Error(), + } + responseText, err := json.Marshal(response) + if err != nil { + ErrorResponse(w, 1, err) + } + + w.Write(responseText) +}