New error handling added to module resource in package data. Minor coding improvements.
This commit is contained in:
@@ -28,12 +28,18 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
const (
|
||||
ResourceItemNotFound = "Resource Item Not Found"
|
||||
NonIntegerValue = "Non Integer Value"
|
||||
InvalidStatusCode = "Invalid Status Code"
|
||||
)
|
||||
|
||||
func getRequestBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
w.Header().Add("Content-Type", "application/octet-stream")
|
||||
n, err := io.Copy(w, h.Request.Body)
|
||||
if err != nil {
|
||||
if n == 0 {
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, "Internal Server Error", w)
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), w)
|
||||
} else {
|
||||
// Only way to abort current connection as of go 1.13
|
||||
// https://github.com/golang/go/issues/16542
|
||||
@@ -65,7 +71,7 @@ func getRequestMatches(w http.ResponseWriter, r *http.Request, h *model.Handler)
|
||||
if value, ok := vars[name]; ok {
|
||||
_, _ = w.Write([]byte(value))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +81,7 @@ func getRequestParams(w http.ResponseWriter, r *http.Request, h *model.Handler)
|
||||
if values, ok := h.Request.URL.Query()[name]; ok {
|
||||
_, _ = w.Write([]byte(values[0]))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +91,7 @@ func getRequestHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler)
|
||||
if values, ok := h.Request.Header[textproto.CanonicalMIMEHeaderKey(name)]; ok {
|
||||
_, _ = w.Write([]byte(values[0]))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +101,7 @@ func getRequestCookies(w http.ResponseWriter, r *http.Request, h *model.Handler)
|
||||
if cookie, err := h.Request.Cookie(name); err == nil {
|
||||
_, _ = w.Write([]byte(cookie.Value))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,11 +117,11 @@ func getRequestForm(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
// We tried to exercise this execution path but didn't know how.
|
||||
err := h.Request.ParseForm()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
} else if values, ok := h.Request.Form[name]; ok {
|
||||
_, _ = w.Write([]byte(values[0]))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +132,7 @@ func getRequestFileName(w http.ResponseWriter, r *http.Request, h *model.Handler
|
||||
if err == nil {
|
||||
_, _ = w.Write([]byte(header.Filename))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +143,7 @@ func getRequestFileContent(w http.ResponseWriter, r *http.Request, h *model.Hand
|
||||
if err == nil {
|
||||
_, _ = io.Copy(w, file)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
srverrors.WriteErrorResponse(http.StatusNotFound, ResourceItemNotFound, w)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,17 +152,16 @@ func getRequestFileContent(w http.ResponseWriter, r *http.Request, h *model.Hand
|
||||
func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
sb, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), w)
|
||||
return
|
||||
}
|
||||
|
||||
si, err := strconv.Atoi(string(sb))
|
||||
if http.StatusText(si) == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
} else if err == nil {
|
||||
h.Writer.WriteHeader(int(si))
|
||||
if si, err := strconv.Atoi(string(sb)); err != nil {
|
||||
srverrors.WriteErrorResponse(http.StatusUnprocessableEntity, NonIntegerValue, w)
|
||||
} else if http.StatusText(si) == "" {
|
||||
srverrors.WriteErrorResponse(http.StatusBadRequest, InvalidStatusCode, w)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
h.Writer.WriteHeader(int(si))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +169,7 @@ func setResponseHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler
|
||||
name := mux.Vars(r)["name"]
|
||||
vb, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -180,7 +185,7 @@ func setResponseCookies(w http.ResponseWriter, r *http.Request, h *model.Handler
|
||||
name := mux.Vars(r)["name"]
|
||||
vb, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -193,6 +198,6 @@ func setResponseBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
||||
if n > 0 {
|
||||
panic("Truncated body")
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
srverrors.WriteErrorResponse(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), w)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user