fix return value for blob & manifest not found

When to delete an non exist blob/manifest, the API has to return a 404 instead of 500

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-07-02 19:15:00 +08:00
parent 264bd02892
commit 95bee9a0cc
2 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ package api
import (
"encoding/json"
"github.com/docker/distribution/registry/storage/driver"
"github.com/goharbor/harbor/src/lib/errors"
lib_http "github.com/goharbor/harbor/src/lib/http"
"net/http"
@ -38,6 +39,9 @@ func HandleBadRequest(w http.ResponseWriter, err error) {
// HandleError ...
func HandleError(w http.ResponseWriter, err error) {
if _, ok := err.(driver.PathNotFoundError); ok {
err = errors.New(nil).WithCode(errors.NotFoundCode).WithMessage(err.Error())
}
lib_http.SendError(w, err)
}

View File

@ -15,6 +15,7 @@
package api
import (
"github.com/docker/distribution/registry/storage/driver"
"github.com/goharbor/harbor/src/lib/errors"
"net/http"
"net/http/httptest"
@ -47,4 +48,10 @@ func TestHandleError(t *testing.T) {
t.Errorf("unexpected status code: %d != %d", w.Code, http.StatusInternalServerError)
}
w = httptest.NewRecorder()
HandleError(w, driver.PathNotFoundError{Path: "/blobstore/nonexist"})
if w.Code != http.StatusNotFound {
t.Errorf("unexpected status code: %d != %d", w.Code, http.StatusNotFound)
}
}