mirror of https://github.com/minio/minio.git
Rewrite if-else chains to switch statements (#7382)
This commit is contained in:
parent
6702d23d52
commit
3d29ab4059
|
@ -319,9 +319,8 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
|
|||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
perfType := vars["perfType"]
|
||||
|
||||
if perfType == "drive" {
|
||||
switch perfType := vars["perfType"]; perfType {
|
||||
case "drive":
|
||||
info := objectAPI.StorageInfo(ctx)
|
||||
if !(info.Backend.Type == BackendFS || info.Backend.Type == BackendErasure) {
|
||||
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL)
|
||||
|
@ -344,7 +343,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
|
|||
// Reply with performance information (across nodes in a
|
||||
// distributed setup) as json.
|
||||
writeSuccessResponseJSON(w, jsonBytes)
|
||||
} else if perfType == "cpu" {
|
||||
case "cpu":
|
||||
// Get CPU load details from local server's cpu(s)
|
||||
cpu := localEndpointsCPULoad(globalEndpoints)
|
||||
// Notify all other Minio peers to report cpu load numbers
|
||||
|
@ -361,7 +360,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
|
|||
// Reply with cpu load information (across nodes in a
|
||||
// distributed setup) as json.
|
||||
writeSuccessResponseJSON(w, jsonBytes)
|
||||
} else if perfType == "mem" {
|
||||
case "mem":
|
||||
// Get mem usage details from local server(s)
|
||||
m := localEndpointsMemUsage(globalEndpoints)
|
||||
// Notify all other Minio peers to report mem usage numbers
|
||||
|
@ -378,7 +377,7 @@ func (a adminAPIHandlers) PerfInfoHandler(w http.ResponseWriter, r *http.Request
|
|||
// Reply with mem usage information (across nodes in a
|
||||
// distributed setup) as json.
|
||||
writeSuccessResponseJSON(w, jsonBytes)
|
||||
} else {
|
||||
default:
|
||||
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMethodNotAllowed), r.URL)
|
||||
}
|
||||
}
|
||||
|
|
35
cmd/posix.go
35
cmd/posix.go
|
@ -1183,18 +1183,19 @@ func (s *posix) StatFile(volume, path string) (file FileInfo, err error) {
|
|||
}
|
||||
st, err := os.Stat((filePath))
|
||||
if err != nil {
|
||||
// File is really not found.
|
||||
if os.IsNotExist(err) {
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// File is really not found.
|
||||
return FileInfo{}, errFileNotFound
|
||||
} else if isSysErrIO(err) {
|
||||
case isSysErrIO(err):
|
||||
return FileInfo{}, errFaultyDisk
|
||||
} else if isSysErrNotDir(err) {
|
||||
case isSysErrNotDir(err):
|
||||
// File path cannot be verified since one of the parents is a file.
|
||||
return FileInfo{}, errFileNotFound
|
||||
default:
|
||||
// Return all errors here.
|
||||
return FileInfo{}, err
|
||||
}
|
||||
|
||||
// Return all errors here.
|
||||
return FileInfo{}, err
|
||||
}
|
||||
// If its a directory its not a regular file.
|
||||
if st.Mode().IsDir() {
|
||||
|
@ -1219,21 +1220,21 @@ func deleteFile(basePath, deletePath string) error {
|
|||
|
||||
// Attempt to remove path.
|
||||
if err := os.Remove((deletePath)); err != nil {
|
||||
// Ignore errors if the directory is not empty. The server relies on
|
||||
// this functionality, and sometimes uses recursion that should not
|
||||
// error on parent directories.
|
||||
if isSysErrNotEmpty(err) {
|
||||
switch {
|
||||
case isSysErrNotEmpty(err):
|
||||
// Ignore errors if the directory is not empty. The server relies on
|
||||
// this functionality, and sometimes uses recursion that should not
|
||||
// error on parent directories.
|
||||
return nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
case os.IsNotExist(err):
|
||||
return errFileNotFound
|
||||
} else if os.IsPermission(err) {
|
||||
case os.IsPermission(err):
|
||||
return errFileAccessDenied
|
||||
} else if isSysErrIO(err) {
|
||||
case isSysErrIO(err):
|
||||
return errFaultyDisk
|
||||
default:
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Trailing slash is removed when found to ensure
|
||||
|
|
|
@ -1798,67 +1798,50 @@ func toJSONError(err error, params ...string) (jerr *json2.Error) {
|
|||
|
||||
// toWebAPIError - convert into error into APIError.
|
||||
func toWebAPIError(err error) APIError {
|
||||
if err == errAuthentication {
|
||||
return APIError{
|
||||
Code: "AccessDenied",
|
||||
HTTPStatusCode: http.StatusForbidden,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errServerNotInitialized {
|
||||
switch err {
|
||||
case errServerNotInitialized:
|
||||
return APIError{
|
||||
Code: "XMinioServerNotInitialized",
|
||||
HTTPStatusCode: http.StatusServiceUnavailable,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == auth.ErrInvalidAccessKeyLength {
|
||||
case errAuthentication, auth.ErrInvalidAccessKeyLength, auth.ErrInvalidSecretKeyLength, errInvalidAccessKeyID:
|
||||
return APIError{
|
||||
Code: "AccessDenied",
|
||||
HTTPStatusCode: http.StatusForbidden,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == auth.ErrInvalidSecretKeyLength {
|
||||
return APIError{
|
||||
Code: "AccessDenied",
|
||||
HTTPStatusCode: http.StatusForbidden,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errInvalidAccessKeyID {
|
||||
return APIError{
|
||||
Code: "AccessDenied",
|
||||
HTTPStatusCode: http.StatusForbidden,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errSizeUnspecified {
|
||||
case errSizeUnspecified:
|
||||
return APIError{
|
||||
Code: "InvalidRequest",
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errChangeCredNotAllowed {
|
||||
case errChangeCredNotAllowed:
|
||||
return APIError{
|
||||
Code: "MethodNotAllowed",
|
||||
HTTPStatusCode: http.StatusMethodNotAllowed,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errInvalidBucketName {
|
||||
case errInvalidBucketName:
|
||||
return APIError{
|
||||
Code: "InvalidBucketName",
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errInvalidArgument {
|
||||
case errInvalidArgument:
|
||||
return APIError{
|
||||
Code: "InvalidArgument",
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
Description: err.Error(),
|
||||
}
|
||||
} else if err == errEncryptedObject {
|
||||
case errEncryptedObject:
|
||||
return getAPIError(ErrSSEEncryptedObject)
|
||||
} else if err == errInvalidEncryptionParameters {
|
||||
case errInvalidEncryptionParameters:
|
||||
return getAPIError(ErrInvalidEncryptionParameters)
|
||||
} else if err == errObjectTampered {
|
||||
case errObjectTampered:
|
||||
return getAPIError(ErrObjectTampered)
|
||||
} else if err == errMethodNotAllowed {
|
||||
case errMethodNotAllowed:
|
||||
return getAPIError(ErrMethodNotAllowed)
|
||||
}
|
||||
|
||||
|
|
|
@ -549,11 +549,12 @@ func timestampCast(v *Value) (t time.Time, _ error) {
|
|||
|
||||
func boolCast(v *Value) (b bool, _ error) {
|
||||
sToB := func(s string) (bool, error) {
|
||||
if s == "true" {
|
||||
switch s {
|
||||
case "true":
|
||||
return true, nil
|
||||
} else if s == "false" {
|
||||
case "false":
|
||||
return false, nil
|
||||
} else {
|
||||
default:
|
||||
return false, errCastFailure("cannot cast to Bool")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue