engine: resources: Add an http error wrapper
This makes it easier to return errors more succinctly.
This commit is contained in:
@@ -244,8 +244,7 @@ func (obj *HTTPServerRes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
handle, err := os.Open(p)
|
handle, err := os.Open(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
obj.init.Logf("could not open: %s", p)
|
obj.init.Logf("could not open: %s", p)
|
||||||
msg, httpStatus := toHTTPError(err)
|
sendHTTPError(w, err)
|
||||||
http.Error(w, msg, httpStatus)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -960,16 +959,14 @@ func (obj *HTTPFileRes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
absPath, err := safepath.ParseIntoAbsPath(requestPath)
|
absPath, err := safepath.ParseIntoAbsPath(requestPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
obj.init.Logf("invalid input path: %s", requestPath)
|
obj.init.Logf("invalid input path: %s", requestPath)
|
||||||
msg, httpStatus := toHTTPError(err)
|
sendHTTPError(w, err)
|
||||||
http.Error(w, msg, httpStatus)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
handle, err := obj.getContent(absPath)
|
handle, err := obj.getContent(absPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
obj.init.Logf("could not get content for: %s", requestPath)
|
obj.init.Logf("could not get content for: %s", requestPath)
|
||||||
msg, httpStatus := toHTTPError(err)
|
sendHTTPError(w, err)
|
||||||
http.Error(w, msg, httpStatus)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1123,3 +1120,9 @@ func toHTTPError(err error) (msg string, httpStatus int) {
|
|||||||
//return "500 Internal Server Error", http.StatusInternalServerError
|
//return "500 Internal Server Error", http.StatusInternalServerError
|
||||||
return http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError
|
return http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendHTTPError is a helper function for sending an http error response.
|
||||||
|
func sendHTTPError(w http.ResponseWriter, err error) {
|
||||||
|
msg, httpStatus := toHTTPError(err)
|
||||||
|
http.Error(w, msg, httpStatus)
|
||||||
|
}
|
||||||
|
|||||||
@@ -122,8 +122,7 @@ func (obj *HTTPFlagRes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
//requestPath := req.URL.Path
|
//requestPath := req.URL.Path
|
||||||
//if err := req.ParseForm(); err != nil { // needed to access querystring
|
//if err := req.ParseForm(); err != nil { // needed to access querystring
|
||||||
// msg, httpStatus := toHTTPError(err)
|
// sendHTTPError(w, err)
|
||||||
// http.Error(w, msg, httpStatus)
|
|
||||||
// return
|
// return
|
||||||
//}
|
//}
|
||||||
if obj.Key != "" {
|
if obj.Key != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user