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 <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-08-26 14:47:28 +08:00
parent a80969e7af
commit f343b2ec45
6 changed files with 23 additions and 12 deletions

View File

@ -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'

View File

@ -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))
}

View File

@ -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))
}
}

View File

@ -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
}

View File

@ -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)),

View File

@ -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)),