Only return "Not Found" status if file does not exist

All other errors are now turned into a internal server error and are
logged.
This commit is contained in:
Michael Eischer 2022-08-29 22:26:34 +02:00
parent 9f4fba2c21
commit 991c459be3

View file

@ -257,10 +257,7 @@ func (h *Handler) checkConfig(w http.ResponseWriter, r *http.Request) {
st, err := os.Stat(cfg) st, err := os.Stat(cfg)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -276,10 +273,7 @@ func (h *Handler) getConfig(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadFile(cfg) bytes, err := ioutil.ReadFile(cfg)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -331,14 +325,7 @@ func (h *Handler) deleteConfig(w http.ResponseWriter, r *http.Request) {
cfg := h.getSubPath("config") cfg := h.getSubPath("config")
if err := os.Remove(cfg); err != nil { if err := os.Remove(cfg); err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
if os.IsNotExist(err) {
httpDefaultError(w, http.StatusNotFound)
} else {
h.internalServerError(w, err)
}
return return
} }
} }
@ -378,10 +365,7 @@ func (h *Handler) listBlobsV1(w http.ResponseWriter, r *http.Request) {
items, err := ioutil.ReadDir(path) items, err := ioutil.ReadDir(path)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -392,10 +376,7 @@ func (h *Handler) listBlobsV1(w http.ResponseWriter, r *http.Request) {
var subitems []os.FileInfo var subitems []os.FileInfo
subitems, err = ioutil.ReadDir(subpath) subitems, err = ioutil.ReadDir(subpath)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
for _, f := range subitems { for _, f := range subitems {
@ -439,10 +420,7 @@ func (h *Handler) listBlobsV2(w http.ResponseWriter, r *http.Request) {
items, err := ioutil.ReadDir(path) items, err := ioutil.ReadDir(path)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -453,10 +431,7 @@ func (h *Handler) listBlobsV2(w http.ResponseWriter, r *http.Request) {
var subitems []os.FileInfo var subitems []os.FileInfo
subitems, err = ioutil.ReadDir(subpath) subitems, err = ioutil.ReadDir(subpath)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
for _, f := range subitems { for _, f := range subitems {
@ -493,10 +468,7 @@ func (h *Handler) checkBlob(w http.ResponseWriter, r *http.Request) {
st, err := os.Stat(path) st, err := os.Stat(path)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -519,10 +491,7 @@ func (h *Handler) getBlob(w http.ResponseWriter, r *http.Request) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
httpDefaultError(w, http.StatusNotFound)
return return
} }
@ -721,14 +690,7 @@ func (h *Handler) deleteBlob(w http.ResponseWriter, r *http.Request) {
} }
if err := os.Remove(path); err != nil { if err := os.Remove(path); err != nil {
if h.opt.Debug { h.fileAccessError(w, err)
log.Print(err)
}
if os.IsNotExist(err) {
httpDefaultError(w, http.StatusNotFound)
} else {
h.internalServerError(w, err)
}
return return
} }
@ -770,7 +732,7 @@ func (h *Handler) createRepo(w http.ResponseWriter, r *http.Request) {
} }
} }
// internalServerError is called to repot an internal server error. // internalServerError is called to report an internal server error.
// The error message will be reported in the server logs. If PanicOnError // The error message will be reported in the server logs. If PanicOnError
// is set, this will panic instead, which makes debugging easier. // is set, this will panic instead, which makes debugging easier.
func (h *Handler) internalServerError(w http.ResponseWriter, err error) { func (h *Handler) internalServerError(w http.ResponseWriter, err error) {
@ -780,3 +742,18 @@ func (h *Handler) internalServerError(w http.ResponseWriter, err error) {
} }
httpDefaultError(w, http.StatusInternalServerError) httpDefaultError(w, http.StatusInternalServerError)
} }
// internalServerError is called to report an error that occurred while
// accessing a file. If the does not exist, the corresponding http status code
// will be returned to the client. All other errors are passed on to
// internalServerError
func (h *Handler) fileAccessError(w http.ResponseWriter, err error) {
if h.opt.Debug {
log.Print(err)
}
if errors.Is(err, os.ErrNotExist) {
httpDefaultError(w, http.StatusNotFound)
} else {
h.internalServerError(w, err)
}
}