0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

errors: compliance requires error codes to be string enum constants.

This commit is contained in:
Ramkumar Chinchani 2020-01-30 23:54:05 -08:00
parent 8803c5f99b
commit 48fb4967a2

View file

@ -1,9 +1,11 @@
package api package api
import "github.com/anuvu/zot/errors" import (
"github.com/anuvu/zot/errors"
)
type Error struct { type Error struct {
Code ErrorCode `json:"code"` Code string `json:"code"`
Message string `json:"message"` Message string `json:"message"`
Description string `json:"description"` Description string `json:"description"`
Detail interface{} `json:"detail,omitempty"` Detail interface{} `json:"detail,omitempty"`
@ -34,6 +36,27 @@ const (
UNSUPPORTED UNSUPPORTED
) )
func (e ErrorCode) String() string {
m := map[ErrorCode]string{
BLOB_UNKNOWN: "BLOB_UNKNOWN",
BLOB_UPLOAD_INVALID: "BLOB_UPLOAD_INVALID",
BLOB_UPLOAD_UNKNOWN: "BLOB_UPLOAD_UNKNOWN",
DIGEST_INVALID: "DIGEST_INVALID",
MANIFEST_BLOB_UNKNOWN: "MANIFEST_BLOB_UNKNOWN",
MANIFEST_INVALID: "MANIFEST_INVALID",
MANIFEST_UNKNOWN: "MANIFEST_UNKNOWN",
MANIFEST_UNVERIFIED: "MANIFEST_UNVERIFIED",
NAME_INVALID: "NAME_INVALID",
NAME_UNKNOWN: "NAME_UNKNOWN",
SIZE_INVALID: "SIZE_INVALID",
TAG_INVALID: "TAG_INVALID",
UNAUTHORIZED: "UNAUTHORIZED",
DENIED: "DENIED",
UNSUPPORTED: "UNSUPPORTED",
}
return m[e]
}
func NewError(code ErrorCode, detail ...interface{}) Error { func NewError(code ErrorCode, detail ...interface{}) Error {
var errMap = map[ErrorCode]Error{ var errMap = map[ErrorCode]Error{
BLOB_UNKNOWN: { BLOB_UNKNOWN: {
@ -135,8 +158,18 @@ func NewError(code ErrorCode, detail ...interface{}) Error {
panic(errors.ErrUnknownCode) panic(errors.ErrUnknownCode)
} }
e.Code = code e.Code = code.String()
e.Detail = detail e.Detail = detail
return e return e
} }
func NewErrorList(errors ...Error) ErrorList {
el := make([]*Error, 0)
for _, e := range errors {
el = append(el, &e)
}
return ErrorList{el}
}