config: return XMinioConfigNotFound code for non existing config (#16065)

This commit is contained in:
Anis Elleuch 2022-11-18 19:28:14 +01:00 committed by GitHub
parent 58ec835af0
commit 993e586855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 12 deletions

View File

@ -84,7 +84,13 @@ func toAdminAPIErr(ctx context.Context, err error) APIError {
Description: e.Error(), Description: e.Error(),
HTTPStatusCode: http.StatusBadRequest, HTTPStatusCode: http.StatusBadRequest,
} }
case config.Error: case config.ErrConfigNotFound:
apiErr = APIError{
Code: "XMinioConfigNotFoundError",
Description: e.Error(),
HTTPStatusCode: http.StatusNotFound,
}
case config.ErrConfigGeneric:
apiErr = APIError{ apiErr = APIError{
Code: "XMinioConfigError", Code: "XMinioConfigError",
Description: e.Error(), Description: e.Error(),

View File

@ -31,19 +31,45 @@ import (
"github.com/minio/pkg/env" "github.com/minio/pkg/env"
) )
// Error config error type // ErrorConfig holds the config error types
type Error struct { type ErrorConfig interface {
Err string ErrConfigGeneric | ErrConfigNotFound
} }
// Errorf - formats according to a format specifier and returns // ErrConfigGeneric is a generic config type
// the string as a value that satisfies error of type config.Error type ErrConfigGeneric struct {
func Errorf(format string, a ...interface{}) error { msg string
return Error{Err: fmt.Sprintf(format, a...)}
} }
func (e Error) Error() string { func (ge *ErrConfigGeneric) setMsg(msg string) {
return e.Err ge.msg = msg
}
func (ge ErrConfigGeneric) Error() string {
return ge.msg
}
// ErrConfigNotFound is an error to indicate
// that a config parameter is not found
type ErrConfigNotFound struct {
ErrConfigGeneric
}
// Error creates an error message and wraps
// it with the error type specified in the type parameter
func Error[T ErrorConfig, PT interface {
*T
setMsg(string)
}](format string, vals ...interface{},
) T {
pt := PT(new(T))
pt.setMsg(fmt.Sprintf(format, vals...))
return *pt
}
// Errorf formats an error and returns it as a generic config error
func Errorf(format string, vals ...interface{}) ErrConfigGeneric {
return Error[ErrConfigGeneric](format, vals...)
} }
// Default keys // Default keys
@ -739,7 +765,7 @@ func (c Config) DelKVS(s string) error {
ck, ok := c[subSys][tgt] ck, ok := c[subSys][tgt]
if !ok { if !ok {
return Errorf("sub-system %s:%s already deleted or does not exist", subSys, tgt) return Error[ErrConfigNotFound]("sub-system %s:%s already deleted or does not exist", subSys, tgt)
} }
if len(inputs) == 2 { if len(inputs) == 2 {
@ -748,7 +774,7 @@ func (c Config) DelKVS(s string) error {
for _, delKey := range strings.Fields(inputs[1]) { for _, delKey := range strings.Fields(inputs[1]) {
_, ok := currKVS.Lookup(delKey) _, ok := currKVS.Lookup(delKey)
if !ok { if !ok {
return Errorf("key %s doesn't exist", delKey) return Error[ErrConfigNotFound]("key %s doesn't exist", delKey)
} }
defVal, isDef := defKVS.Lookup(delKey) defVal, isDef := defKVS.Lookup(delKey)
if isDef { if isDef {