diff --git a/src/core/middlewares/countquota/handler.go b/src/core/middlewares/countquota/handler.go index 1b05a4cf5..7564b1a5e 100644 --- a/src/core/middlewares/countquota/handler.go +++ b/src/core/middlewares/countquota/handler.go @@ -21,6 +21,7 @@ import ( "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/middlewares/interceptor" "github.com/goharbor/harbor/src/core/middlewares/util" + "strings" ) type countQuotaHandler struct { @@ -57,6 +58,10 @@ func (h *countQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) if err := interceptor.HandleRequest(req); err != nil { log.Warningf("Error occurred when to handle request in count quota handler: %v", err) + if strings.Contains(err.Error(), "resource overflow the hard limit") { + http.Error(rw, util.MarshalError("DENIED", fmt.Sprintf("Not enough quota is available to process the request: %v", err)), http.StatusForbidden) + return + } http.Error(rw, util.MarshalError("InternalError", fmt.Sprintf("Error occurred when to handle request in count quota handler: %v", err)), http.StatusInternalServerError) return diff --git a/src/core/middlewares/countquota/handler_test.go b/src/core/middlewares/countquota/handler_test.go index a2ebb5a69..84833e4de 100644 --- a/src/core/middlewares/countquota/handler_test.go +++ b/src/core/middlewares/countquota/handler_test.go @@ -202,12 +202,12 @@ func (suite *HandlerSuite) TestPutManifestFailed() { }() next := func(w http.ResponseWriter, req *http.Request) { - w.WriteHeader(http.StatusInternalServerError) + w.WriteHeader(http.StatusForbidden) } dgt := digest.FromString(randomString(15)).String() code := doPutManifestRequest(projectID, projectName, "photon", "latest", dgt, next) - suite.Equal(http.StatusInternalServerError, code) + suite.Equal(http.StatusForbidden, code) suite.checkCountUsage(0, projectID) total, err := dao.GetTotalOfArtifacts(&models.ArtifactQuery{Digest: dgt}) diff --git a/src/core/middlewares/sizequota/handler.go b/src/core/middlewares/sizequota/handler.go index 244e55589..638d560e3 100644 --- a/src/core/middlewares/sizequota/handler.go +++ b/src/core/middlewares/sizequota/handler.go @@ -21,6 +21,7 @@ import ( "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/middlewares/interceptor" "github.com/goharbor/harbor/src/core/middlewares/util" + "strings" ) type sizeQuotaHandler struct { @@ -57,6 +58,10 @@ func (h *sizeQuotaHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) if err := interceptor.HandleRequest(req); err != nil { log.Warningf("Error occurred when to handle request in size quota handler: %v", err) + if strings.Contains(err.Error(), "resource overflow the hard limit") { + http.Error(rw, util.MarshalError("DENIED", fmt.Sprintf("Not enough quota is available to process the request: %v", err)), http.StatusForbidden) + return + } http.Error(rw, util.MarshalError("InternalError", fmt.Sprintf("Error occurred when to handle request in size quota handler: %v", err)), http.StatusInternalServerError) return