Fix Error Code for ObjectTagging Parsing (#8971)

Also add Mint tests
This commit is contained in:
Nitish Tiwari
2020-02-12 07:12:28 +05:30
committed by GitHub
parent 33767266e7
commit 7e819d00ea
4 changed files with 215 additions and 11 deletions

View File

@@ -23,13 +23,14 @@ import (
// Error is the generic type for any error happening during tag
// parsing.
type Error struct {
err error
err error
code string
}
// Errorf - formats according to a format specifier and returns
// the string as a value that satisfies error of type tagging.Error
func Errorf(format string, a ...interface{}) error {
return Error{err: fmt.Errorf(format, a...)}
func Errorf(format, code string, a ...interface{}) error {
return Error{err: fmt.Errorf(format, a...), code: code}
}
// Unwrap the internal error.
@@ -42,3 +43,11 @@ func (e Error) Error() string {
}
return e.err.Error()
}
// Code returns appropriate error code.
func (e Error) Code() string {
if e.code == "" {
return "BadRequest"
}
return e.code
}

View File

@@ -33,10 +33,10 @@ const (
// errors returned by tagging package
var (
ErrTooManyTags = Errorf("Cannot have more than 10 object tags")
ErrInvalidTagKey = Errorf("The TagKey you have provided is invalid")
ErrInvalidTagValue = Errorf("The TagValue you have provided is invalid")
ErrInvalidTag = Errorf("Cannot provide multiple Tags with the same key")
ErrTooManyTags = Errorf("Object tags cannot be greater than 10", "BadRequest")
ErrInvalidTagKey = Errorf("The TagKey you have provided is invalid", "InvalidTag")
ErrInvalidTagValue = Errorf("The TagValue you have provided is invalid", "InvalidTag")
ErrInvalidTag = Errorf("Cannot provide multiple Tags with the same key", "InvalidTag")
)
// Tagging - object tagging interface
@@ -51,15 +51,15 @@ func (t Tagging) Validate() error {
if len(t.TagSet.Tags) > maxTags {
return ErrTooManyTags
}
if t.TagSet.ContainsDuplicateTag() {
return ErrInvalidTag
}
// Validate all the rules in the tagging config
for _, ts := range t.TagSet.Tags {
if err := ts.Validate(); err != nil {
return err
}
}
if t.TagSet.ContainsDuplicateTag() {
return ErrInvalidTag
}
return nil
}