From f343b2ec45a75433e54c2286ba80e7cf2b8ea653 Mon Sep 17 00:00:00 2001 From: wang yan Date: Mon, 26 Aug 2019 14:47:28 +0800 Subject: [PATCH] Revise quota errors to make it more readable 1, fix #8802, update the error formet 2, fix #8807, raise the real retag error to UI 3, fix #8832, raise the real chart error to chart client & ut Signed-off-by: wang yan --- docs/swagger.yaml | 4 +++- src/common/quota/errors.go | 6 +++--- src/core/api/repository.go | 4 ++++ src/core/middlewares/chart/handler.go | 9 +++++++-- src/core/middlewares/countquota/handler.go | 6 +++--- src/core/middlewares/sizequota/handler.go | 6 +++--- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 84b6665fc..8ea6ed272 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1308,6 +1308,8 @@ paths: description: Invalid image values provided. '401': description: User has no permission to the source project or destination project. + '403': + description: Forbiden as quota exceeded. '404': description: Project or repository not found. '409': @@ -5146,7 +5148,7 @@ definitions: allOf: - $ref: '#/definitions/ChartAPIError' ForbiddenChartAPIError: - description: Operation is forbidden + description: Operation is forbidden or quota exceeded type: object allOf: - $ref: '#/definitions/ChartAPIError' diff --git a/src/common/quota/errors.go b/src/common/quota/errors.go index c828734dd..162cf536f 100644 --- a/src/common/quota/errors.go +++ b/src/common/quota/errors.go @@ -79,14 +79,14 @@ func (e *ResourceOverflow) Error() string { ) if e.NewUsed > e.CurrentUsed { - op = "add" + op = "adding" delta = e.NewUsed - e.CurrentUsed } else { - op = "subtract" + op = "subtracting" delta = e.CurrentUsed - e.NewUsed } - return fmt.Sprintf("%s %s of %s resource overflow the hard limit, current usage is %s and hard limit is %s", + return fmt.Sprintf("%s %s of %s resource, which when updated to current usage of %s will exceed the configured upper limit of %s.", op, resource.FormatValue(delta), resource, resource.FormatValue(e.CurrentUsed), resource.FormatValue(e.HardLimit)) } diff --git a/src/core/api/repository.go b/src/core/api/repository.go index b7219dd59..8a40d5f49 100755 --- a/src/core/api/repository.go +++ b/src/core/api/repository.go @@ -505,6 +505,10 @@ func (ra *RepositoryAPI) Retag() { Repo: repo, Tag: request.Tag, }); err != nil { + if e, ok := err.(*commonhttp.Error); ok { + ra.RenderFormattedError(e.Code, e.Message) + return + } ra.SendInternalServerError(fmt.Errorf("%v", err)) } } diff --git a/src/core/middlewares/chart/handler.go b/src/core/middlewares/chart/handler.go index dd1fa583b..87d14a6fb 100644 --- a/src/core/middlewares/chart/handler.go +++ b/src/core/middlewares/chart/handler.go @@ -18,6 +18,7 @@ import ( "fmt" "net/http" + "github.com/goharbor/harbor/src/common/quota" "github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/core/middlewares/interceptor" "github.com/goharbor/harbor/src/core/middlewares/util" @@ -44,7 +45,7 @@ func New(next http.Handler, builders ...interceptor.Builder) http.Handler { func (h *chartHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { interceptor, err := h.getInterceptor(req) if err != nil { - http.Error(rw, util.MarshalError("InternalError", fmt.Sprintf("Error occurred when to handle request in chart count quota handler: %v", err)), + http.Error(rw, fmt.Sprintf("Error occurred when to handle request in chart count quota handler: %v", err), http.StatusInternalServerError) return } @@ -56,7 +57,11 @@ func (h *chartHandler) 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) - http.Error(rw, util.MarshalError("InternalError", fmt.Sprintf("Error occurred when to handle request in chart count quota handler: %v", err)), + if _, ok := err.(quota.Errors); ok { + http.Error(rw, fmt.Sprintf("Quota exceeded when processing the request of %v", err), http.StatusForbidden) + return + } + http.Error(rw, fmt.Sprintf("Error occurred when to handle request in chart count quota handler: %v", err), http.StatusInternalServerError) return } diff --git a/src/core/middlewares/countquota/handler.go b/src/core/middlewares/countquota/handler.go index 7564b1a5e..fc8e402d6 100644 --- a/src/core/middlewares/countquota/handler.go +++ b/src/core/middlewares/countquota/handler.go @@ -18,10 +18,10 @@ import ( "fmt" "net/http" + "github.com/goharbor/harbor/src/common/quota" "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 { @@ -58,8 +58,8 @@ 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) + if _, ok := err.(quota.Errors); ok { + http.Error(rw, util.MarshalError("DENIED", fmt.Sprintf("Quota exceeded when processing the request of %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)), diff --git a/src/core/middlewares/sizequota/handler.go b/src/core/middlewares/sizequota/handler.go index 638d560e3..9954a50a0 100644 --- a/src/core/middlewares/sizequota/handler.go +++ b/src/core/middlewares/sizequota/handler.go @@ -18,10 +18,10 @@ import ( "fmt" "net/http" + "github.com/goharbor/harbor/src/common/quota" "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 { @@ -58,8 +58,8 @@ 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) + if _, ok := err.(quota.Errors); ok { + http.Error(rw, util.MarshalError("DENIED", fmt.Sprintf("Quota exceeded when processing the request of %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)),