mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 12:28:01 +08:00
feat: propagate detailed error msgs to client (OCI dist-spec format) (#1681)
Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
+44
-48
@@ -5,16 +5,10 @@ import (
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Description string `json:"description"`
|
||||
Detail interface{} `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
func (e Error) WithMessage(msg string) Error {
|
||||
e.Message = msg
|
||||
|
||||
return e
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Description string `json:"-"`
|
||||
Detail map[string]string `json:"detail"`
|
||||
}
|
||||
|
||||
type ErrorList struct {
|
||||
@@ -66,7 +60,7 @@ func (e ErrorCode) String() string {
|
||||
return errMap[e]
|
||||
}
|
||||
|
||||
func NewError(code ErrorCode, detail ...interface{}) Error {
|
||||
func NewError(code ErrorCode) *Error {
|
||||
errMap := map[ErrorCode]Error{
|
||||
BLOB_UNKNOWN: {
|
||||
Message: "blob unknown to registry",
|
||||
@@ -77,12 +71,12 @@ func NewError(code ErrorCode, detail ...interface{}) Error {
|
||||
|
||||
BLOB_UPLOAD_INVALID: {
|
||||
Message: "blob upload invalid",
|
||||
Description: `The blob upload encountered an error and can no longer proceed.`,
|
||||
Description: "The blob upload encountered an error and can no longer proceed.",
|
||||
},
|
||||
|
||||
BLOB_UPLOAD_UNKNOWN: {
|
||||
Message: "blob upload unknown to registry",
|
||||
Description: `If a blob upload has been cancelled or was never started, this error code MAY be returned.`,
|
||||
Description: "If a blob upload has been cancelled or was never started, this error code MAY be returned.",
|
||||
},
|
||||
|
||||
DIGEST_INVALID: {
|
||||
@@ -94,71 +88,68 @@ func NewError(code ErrorCode, detail ...interface{}) Error {
|
||||
},
|
||||
|
||||
MANIFEST_BLOB_UNKNOWN: {
|
||||
Message: "blob unknown to registry",
|
||||
Description: `This error MAY be returned when a manifest blob is unknown
|
||||
to the registry.`,
|
||||
Message: "blob unknown to registry",
|
||||
Description: "This error MAY be returned when a manifest blob is unknown to the registry.",
|
||||
},
|
||||
|
||||
MANIFEST_INVALID: {
|
||||
Message: "manifest invalid",
|
||||
Description: `During upload, manifests undergo several checks ensuring
|
||||
validity. If those checks fail, this error MAY be returned, unless a more
|
||||
specific error is included. The detail will contain information the failed
|
||||
validation.`,
|
||||
Description: "During upload, manifests undergo several checks ensuring " +
|
||||
"validity. If those checks fail, this error MAY be returned, unless a more " +
|
||||
"specific error is included. The detail will contain information the failed validation.",
|
||||
},
|
||||
|
||||
MANIFEST_UNKNOWN: {
|
||||
Message: "manifest unknown",
|
||||
Description: `This error is returned when the manifest, identified by name
|
||||
and tag is unknown to the repository.`,
|
||||
Description: "This error is returned when the manifest, identified by name " +
|
||||
"and tag is unknown to the repository.",
|
||||
},
|
||||
|
||||
MANIFEST_UNVERIFIED: {
|
||||
Message: "manifest failed signature verification",
|
||||
Description: `During manifest upload, if the manifest fails signature
|
||||
verification, this error will be returned.`,
|
||||
Description: "During manifest upload, if the manifest fails signature " +
|
||||
"verification, this error will be returned.",
|
||||
},
|
||||
|
||||
NAME_INVALID: {
|
||||
Message: "invalid repository name",
|
||||
Description: `Invalid repository name encountered either during manifest
|
||||
validation or any API operation.`,
|
||||
Description: "Invalid repository name encountered either during manifest " +
|
||||
"validation or any API operation.",
|
||||
},
|
||||
|
||||
NAME_UNKNOWN: {
|
||||
Message: "repository name not known to registry",
|
||||
Description: `This is returned if the name used during an operation is unknown to the registry.`,
|
||||
Description: "This is returned if the name used during an operation is unknown to the registry.",
|
||||
},
|
||||
|
||||
SIZE_INVALID: {
|
||||
Message: "provided length did not match content length",
|
||||
Description: "When a layer is uploaded, the provided size will be checked against the uploaded " +
|
||||
"content. If they do not match, this error will be returned.",
|
||||
Description: "When a layer is uploaded, the provided size will be checked against " +
|
||||
"the uploaded content. If they do not match, this error will be returned.",
|
||||
},
|
||||
|
||||
TAG_INVALID: {
|
||||
Message: "manifest tag did not match URI",
|
||||
Description: `During a manifest upload, if the tag in the manifest does
|
||||
not match the uri tag, this error will be returned.`,
|
||||
Description: "During a manifest upload, if the tag in the manifest does " +
|
||||
"not match the uri tag, this error will be returned.",
|
||||
},
|
||||
|
||||
UNAUTHORIZED: {
|
||||
Message: "authentication required",
|
||||
Description: `The access controller was unable to authenticate the client.
|
||||
Often this will be accompanied by a Www-Authenticate HTTP response header
|
||||
indicating how to authenticate.`,
|
||||
Description: "The access controller was unable to authenticate the client." +
|
||||
"Often this will be accompanied by a Www-Authenticate HTTP response header " +
|
||||
"indicating how to authenticate.",
|
||||
},
|
||||
|
||||
DENIED: {
|
||||
Message: "requested access to the resource is denied",
|
||||
Description: `The access controller denied access for the operation on a
|
||||
resource.`,
|
||||
Message: "requested access to the resource is denied",
|
||||
Description: "The access controller denied access for the operation on a resource.",
|
||||
},
|
||||
|
||||
UNSUPPORTED: {
|
||||
Message: "The operation is unsupported.",
|
||||
Description: `The operation was unsupported due to a missing
|
||||
implementation or invalid set of parameters.`,
|
||||
Description: "The operation was unsupported due to a missing " +
|
||||
"implementation or invalid set of parameters.",
|
||||
},
|
||||
|
||||
INVALID_INDEX: {
|
||||
@@ -173,19 +164,24 @@ func NewError(code ErrorCode, detail ...interface{}) Error {
|
||||
}
|
||||
|
||||
err.Code = code.String()
|
||||
err.Detail = detail
|
||||
err.Detail = map[string]string{
|
||||
"description": err.Description,
|
||||
}
|
||||
|
||||
return &err
|
||||
}
|
||||
|
||||
func (err *Error) AddDetail(m map[string]string) *Error {
|
||||
for k, v := range m {
|
||||
err.Detail[k] = v
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NewErrorList(errors ...Error) ErrorList {
|
||||
errList := make([]*Error, 0)
|
||||
err := Error{}
|
||||
|
||||
for _, e := range errors {
|
||||
err = e
|
||||
errList = append(errList, &err)
|
||||
}
|
||||
func NewErrorList(errors ...*Error) ErrorList {
|
||||
var errList []*Error
|
||||
errList = append(errList, errors...)
|
||||
|
||||
return ErrorList{errList}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,6 @@ import (
|
||||
|
||||
func TestUnknownCodeError(t *testing.T) {
|
||||
Convey("Retrieve a new error with unknown code", t, func() {
|
||||
So(func() { _ = apiErr.NewError(123456789, nil) }, ShouldPanic)
|
||||
So(func() { _ = apiErr.NewError(123456789) }, ShouldPanic)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user