Skip to content
Snippets Groups Projects
Commit 5b74f5c6 authored by Bruce Flynn's avatar Bruce Flynn
Browse files

shore up err handling a bit

parent ad0651e9
Branches main
No related tags found
No related merge requests found
Pipeline #4067 passed with stage
in 21 seconds
......@@ -13,6 +13,16 @@ import (
"github.com/spf13/pflag"
)
func httpError(w http.ResponseWriter, r *http.Request, msg string, code int) {
log.Printf("HttpError[%d %s]: %s", code, msg, r.URL)
http.Error(w, msg, code)
}
func httpFail(w http.ResponseWriter, r *http.Request, msg string, err error) {
log.Printf("InternalServerError: %s: %s: %s", msg, err, r.URL)
http.Error(w, msg, http.StatusInternalServerError)
}
func updateIndex() error {
cmd := exec.Command("helm", "repo", "index", "--url", url, dir)
cmd.Stderr = os.Stderr
......@@ -28,33 +38,29 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
st, err := os.Stat(p)
// got an error and the file exists... something interesting perhaps?
if err != nil && !os.IsNotExist(err) {
log.Printf("ERROR: could not stat %s : %s\n", p, err)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
httpError(w, r, "InternalServerError", http.StatusInternalServerError)
return
}
// got a stat result, file must exist
if st != nil {
http.Error(w, "File exists", http.StatusForbidden)
httpError(w, r, "File exists", http.StatusForbidden)
return
}
// file does not exist, proceed with upload
f, err := os.Create(p)
if err != nil {
log.Printf("InternalServerErrror: could not create %s: %s\n", p, err)
http.Error(w, "Could not complete upload", http.StatusInternalServerError)
httpFail(w, r, "Could not complete upload", err)
return
}
if _, err := io.Copy(f, r.Body); err != nil {
log.Printf("InternalServerErrror: could not copy %s: %s\n", p, err)
http.Error(w, "Could not complete upload", http.StatusInternalServerError)
httpFail(w, r, "Could not complete upload", err)
return
}
err = updateIndex()
if err != nil {
log.Printf("ERROR updating index: %s\n", err)
http.Error(w, "Could not update index", http.StatusInternalServerError)
httpError(w, r, "Could not update index", http.StatusInternalServerError)
// best-effort to remove the file if we could not update the index
os.Remove(p)
return
......@@ -80,19 +86,18 @@ helm repo add <name> %s
func handleGetFile(w http.ResponseWriter, r *http.Request) {
p := path.Join(dir, r.URL.Path)
if st, err := os.Stat(p); os.IsNotExist(err) || st.IsDir() {
http.Error(w, "Not Found", http.StatusNotFound)
st, err := os.Stat(p)
if err != nil && os.IsNotExist(err) || st != nil && st.IsDir() {
httpError(w, r, "Not Found", http.StatusNotFound)
return
}
f, err := os.Open(p)
if err != nil {
log.Printf("InternalServerErrror: could not open %s: %s\n", p, err)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
httpFail(w, r, "InternalServerError", err)
return
}
if _, err := io.Copy(w, f); err != nil {
log.Printf("InternalServerErrror: could not copy %s: %s\n", p, err)
http.Error(w, "InternalServerError", http.StatusInternalServerError)
httpFail(w, r, "InternalServerError", err)
return
}
}
......@@ -108,7 +113,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
handleGetFile(w, r)
}
default:
http.Error(w, "Bad Request", http.StatusBadRequest)
httpError(w, r, "Bad Request", http.StatusBadRequest)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment