From c04acf4d500b09e52ef3be85954f01eb4ff29d6f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 13 Jun 2015 21:36:42 -0700 Subject: [PATCH] Convert all errors.New and fmt.Errorf to typed errors --- aliases.go | 4 +-- cmd-access.go | 5 ++- cmd-cat.go | 7 ++-- cmd-config.go | 2 +- cmd-cp.go | 3 +- cmd-diff.go | 3 +- cmd-ls.go | 3 +- cmd-mb.go | 11 +++--- cmd-sync.go | 3 +- cmd-update.go | 3 +- common-methods.go | 8 ++--- cp-url.go | 17 +++++----- errors.go | 68 ++++++++++++++++++++++++++++++-------- hostconfig.go | 2 +- main.go | 3 +- pkg/client/errors.go | 17 +++++++--- pkg/client/fs/fs.go | 3 +- pkg/client/s3/httptrace.go | 3 +- sync-url.go | 9 +++-- 19 files changed, 104 insertions(+), 70 deletions(-) diff --git a/aliases.go b/aliases.go index 5ee96540..65bef755 100644 --- a/aliases.go +++ b/aliases.go @@ -49,7 +49,7 @@ func isValidAliasName(aliasName string) bool { func aliasExpand(aliasedURL string, aliases map[string]string) (newURL string, err error) { u, err := client.Parse(aliasedURL) if err != nil { - return aliasedURL, iodine.New(errInvalidURL{url: aliasedURL}, nil) + return aliasedURL, iodine.New(errInvalidURL{URL: aliasedURL}, nil) } // proper URL if u.Host != "" { @@ -69,7 +69,7 @@ func aliasExpand(aliasedURL string, aliases map[string]string) (newURL string, e trimmedURL := expandedURL + "/" + strings.TrimPrefix(strings.TrimPrefix(splits[1], "/"), "\\") u, err := client.Parse(trimmedURL) if err != nil { - return aliasedURL, iodine.New(errInvalidURL{url: aliasedURL}, nil) + return aliasedURL, iodine.New(errInvalidURL{URL: aliasedURL}, nil) } return u.String(), nil } diff --git a/cmd-access.go b/cmd-access.go index ca7f992d..f0817c65 100644 --- a/cmd-access.go +++ b/cmd-access.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "github.com/minio/cli" @@ -68,7 +67,7 @@ func runAccessCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } config, err := getMcConfig() @@ -89,7 +88,7 @@ func runAccessCmd(ctx *cli.Context) { if !acl.isValidBucketACL() { console.Fatals(ErrorMessage{ Message: "Valid types are [private, public, readonly].", - Error: iodine.New(errors.New("Invalid ACL Type ‘"+acl.String()+"’"), nil), + Error: iodine.New(errInvalidACL{acl: acl.String()}, nil), }) } targetURLs = targetURLs[1:] // 1 or more target URLs diff --git a/cmd-cat.go b/cmd-cat.go index 97afec73..f4d9f3fd 100644 --- a/cmd-cat.go +++ b/cmd-cat.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "io" "os" @@ -69,7 +68,7 @@ func runCatCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } config, err := getMcConfig() @@ -118,9 +117,9 @@ func doCatCmd(sourceURLs []string) (string, error) { // stdout closed by the user. Gracefully exit. return "", nil } - return "Writing data to stdout failed, unexpected problem.. please report this error", iodine.New(e, nil) + return "Writing data to stdout failed, unexpected problem.. please report this error", iodine.New(err, nil) default: - return "Reading data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil) + return "Reading data from source failed: " + url, iodine.New(err, nil) } } } diff --git a/cmd-config.go b/cmd-config.go index bb7ad7e9..e92f9e71 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -154,7 +154,7 @@ func addAlias(aliases []string) (quick.Config, error) { return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) } if !strings.HasPrefix(url, "http") { - return nil, iodine.New(errInvalidURL{url: url}, nil) + return nil, iodine.New(errInvalidURL{URL: url}, nil) } if !isValidAliasName(aliasName) { return nil, iodine.New(errInvalidAliasName{name: aliasName}, nil) diff --git a/cmd-cp.go b/cmd-cp.go index ae3b95c4..6c84aa6d 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "io" "math" @@ -185,7 +184,7 @@ func runCopyCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } diff --git a/cmd-diff.go b/cmd-diff.go index eea12a98..6e67b4e4 100644 --- a/cmd-diff.go +++ b/cmd-diff.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "github.com/minio/cli" @@ -62,7 +61,7 @@ func runDiffCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } config, err := getMcConfig() diff --git a/cmd-ls.go b/cmd-ls.go index e5f20e90..5af553dc 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "github.com/minio/cli" @@ -87,7 +86,7 @@ func runListCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } config, err := getMcConfig() diff --git a/cmd-mb.go b/cmd-mb.go index 87e63464..6d33060c 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "github.com/minio/cli" @@ -64,14 +63,14 @@ func runMakeBucketCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: errors.New("\"mc\" is not configured"), + Error: iodine.New(errNotConfigured{}, nil), }) } config, err := getMcConfig() if err != nil { console.Fatals(ErrorMessage{ Message: "Unable to read config file ‘" + mustGetMcConfigPath() + "’", - Error: err, + Error: iodine.New(err, nil), }) } for _, arg := range ctx.Args() { @@ -81,12 +80,12 @@ func runMakeBucketCmd(ctx *cli.Context) { case errUnsupportedScheme: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unknown type of URL ‘%s’", e.url), - Error: e, + Error: iodine.New(e, nil), }) default: console.Fatals(ErrorMessage{ Message: fmt.Sprintf("Unable to parse argument ‘%s’", arg), - Error: err, + Error: iodine.New(err, nil), }) } } @@ -94,7 +93,7 @@ func runMakeBucketCmd(ctx *cli.Context) { if err != nil { console.Errors(ErrorMessage{ Message: errorMsg, - Error: err, + Error: iodine.New(err, nil), }) } } diff --git a/cmd-sync.go b/cmd-sync.go index e15b7c00..5c842408 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "io" "math" @@ -165,7 +164,7 @@ func runSyncCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } diff --git a/cmd-update.go b/cmd-update.go index 3591bdf0..0e973441 100644 --- a/cmd-update.go +++ b/cmd-update.go @@ -18,7 +18,6 @@ package main import ( "encoding/json" - "errors" "net/http" "runtime" "time" @@ -104,7 +103,7 @@ func runUpdateCmd(ctx *cli.Context) { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } msg, err := doUpdateCheck() diff --git a/common-methods.go b/common-methods.go index 384d7099..10bee2fb 100644 --- a/common-methods.go +++ b/common-methods.go @@ -17,7 +17,6 @@ package main import ( - "errors" "io" "os" "runtime" @@ -105,7 +104,7 @@ func putTargets(targetURLs []string, length int64, reader io.Reader) <-chan erro func getNewClient(urlStr string, auth *hostConfig) (clnt client.Client, err error) { url, err := client.Parse(urlStr) if err != nil { - return nil, iodine.New(errInvalidURL{url: urlStr}, nil) + return nil, iodine.New(errInvalidURL{URL: urlStr}, nil) } switch url.Type { case client.Object: // Minio and S3 compatible object storage @@ -134,7 +133,7 @@ func getNewClient(urlStr string, auth *hostConfig) (clnt client.Client, err erro case client.Filesystem: return fs.New(urlStr) } - return nil, iodine.New(errInvalidURL{url: urlStr}, nil) + return nil, iodine.New(errInvalidURL{URL: urlStr}, nil) } // url2Stat - Returns client, config and its stat Content from the URL @@ -165,7 +164,7 @@ func url2Client(url string) (client.Client, error) { } if urlParse.Path == "" { - return nil, iodine.New(errors.New("invalid path"), nil) + return nil, iodine.New(errInvalidURL{URL: url}, nil) } urlonfig, err := getHostConfig(url) @@ -177,6 +176,7 @@ func url2Client(url string) (client.Client, error) { if err != nil { return nil, iodine.New(err, nil) } + return client, nil } diff --git a/cp-url.go b/cp-url.go index 00521570..291c36df 100644 --- a/cp-url.go +++ b/cp-url.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "path/filepath" "strings" @@ -191,7 +190,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) <-chan cpURLs { if err == nil { if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errTargetIsNotDir{URL: targetURL}, nil)} return } } // Else name is available to create. @@ -225,7 +224,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan cpURLs { defer close(cpURLsCh) if !isURLRecursive(sourceURL) { // Source is not of recursive type. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not recursive.", sourceURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errSourceNotRecursive{URL: sourceURL}, nil)} return } @@ -247,7 +246,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan cpURLs { if !sourceContent.Type.IsDir() { // Source is not a dir. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not a directory.", sourceURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errSourceIsNotDir{URL: sourceURL}, nil)} return } @@ -261,13 +260,13 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan cpURLs { targetContent, err := targetClient.Stat() if err != nil { // Target does not exist. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errTargetNotFound{URL: targetURL}, nil)} return } if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errTargetIsNotDir{URL: targetURL}, nil)} return } @@ -333,17 +332,17 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan cpURLs { targetContent, err := targetClient.Stat() if err != nil { // Target does not exist. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errTargetNotFound{URL: targetURL}, nil)} return } if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errTargetIsNotDir{URL: targetURL}, nil)} return } if sourceURLs == nil { // Source list is empty. - cpURLsCh <- cpURLs{Error: iodine.New(fmt.Errorf("Source list is empty"), nil)} + cpURLsCh <- cpURLs{Error: iodine.New(errSourceListEmpty{}, nil)} return } for _, sourceURL := range sourceURLs { diff --git a/errors.go b/errors.go index 6457c6e8..0b7bb6d7 100644 --- a/errors.go +++ b/errors.go @@ -16,6 +16,20 @@ package main +type errInvalidACL struct { + acl string +} + +func (e errInvalidACL) Error() string { + return "Invalid ACL Type ‘" + e.acl + "’." +} + +type errNotConfigured struct{} + +func (e errNotConfigured) Error() string { + return "‘mc’ not configured." +} + type errNotAnObject struct { url string } @@ -39,14 +53,6 @@ func (e errUnsupportedScheme) Error() string { return "Unsuppported URL scheme: " + e.scheme } -type errInvalidURL struct { - url string -} - -func (e errInvalidURL) Error() string { - return "Invalid URL: " + e.url -} - type errInvalidGlobURL struct { glob string request string @@ -79,7 +85,7 @@ func (e errNoMatchingHost) Error() string { type errConfigExists struct{} func (e errConfigExists) Error() string { - return "Config exists" + return "Config exists." } // errAliasExists - alias exists @@ -88,20 +94,24 @@ type errAliasExists struct { } func (e errAliasExists) Error() string { - return "Alias name: " + e.name + " exists" + return "Alias name: " + e.name + " exists." } -type errInvalidSource struct { +type errInvalidURL struct { URL string } +func (e errInvalidURL) Error() string { + return "Invalid url " + e.URL +} + +type errInvalidSource errInvalidURL + func (e errInvalidSource) Error() string { return "Invalid source " + e.URL } -type errInvalidTarget struct { - URL string -} +type errInvalidTarget errInvalidURL func (e errInvalidTarget) Error() string { return "Invalid target " + e.URL @@ -114,3 +124,33 @@ type errInvalidTheme struct { func (e errInvalidTheme) Error() string { return "Theme " + e.Theme + " is not supported." } + +type errTargetIsNotDir errInvalidURL + +func (e errTargetIsNotDir) Error() string { + return "Target ‘" + e.URL + "’ is not a directory." +} + +type errTargetNotFound errInvalidURL + +func (e errTargetNotFound) Error() string { + return "Target directory ‘" + e.URL + "’ does not exist." +} + +type errSourceNotRecursive errInvalidURL + +func (e errSourceNotRecursive) Error() string { + return "Source ‘" + e.URL + "’ is not recursive." +} + +type errSourceIsNotDir errTargetIsNotDir + +func (e errSourceIsNotDir) Error() string { + return "Source ‘" + e.URL + "’ is not a directory." +} + +type errSourceListEmpty errInvalidArgument + +func (e errSourceListEmpty) Error() string { + return "Source list is empty." +} diff --git a/hostconfig.go b/hostconfig.go index d50f08bc..5a716d6f 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -37,7 +37,7 @@ func getHostConfig(URL string) (*hostConfig, error) { } url, err := client.Parse(URL) if err != nil { - return nil, iodine.New(errInvalidURL{url: URL}, nil) + return nil, iodine.New(errInvalidURL{URL: URL}, nil) } // No host matching or keys needed for filesystem requests if url.Type == client.Filesystem { diff --git a/main.go b/main.go index 5f5240ae..9af9a6eb 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "os" "os/user" @@ -145,7 +144,7 @@ func main() { if !isMcConfigExist() { console.Fatals(ErrorMessage{ Message: "Please run \"mc config generate\"", - Error: iodine.New(errors.New("\"mc\" is not configured"), nil), + Error: iodine.New(errNotConfigured{}, nil), }) } return nil diff --git a/pkg/client/errors.go b/pkg/client/errors.go index 5bac0559..69006f4b 100644 --- a/pkg/client/errors.go +++ b/pkg/client/errors.go @@ -30,22 +30,20 @@ func (e APINotImplemented) Error() string { } // GenericError - generic error -type GenericError struct { - Err error -} +type GenericError struct{} // UnexpectedError - unexpected error type UnexpectedError GenericError func (e UnexpectedError) Error() string { - return e.Err.Error() + ", please report this error" + return "Unexpected error, please report this error at https://github.com/minio/mc/issues" } // InvalidArgument - bad arguments provided type InvalidArgument GenericError func (e InvalidArgument) Error() string { - return e.Err.Error() + return "Invalid argument" } // InvalidRange - invalid range requested @@ -56,3 +54,12 @@ type InvalidRange struct { func (e InvalidRange) Error() string { return "invalid range offset: " + strconv.FormatInt(e.Offset, 10) } + +// InvalidACLType - invalid acl type +type InvalidACLType struct { + ACL string +} + +func (e InvalidACLType) Error() string { + return "invalid acl type: " + e.ACL +} diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 68fe4abd..b5525601 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -17,7 +17,6 @@ package fs import ( - "errors" "io" "os" "path/filepath" @@ -376,7 +375,7 @@ func (f *fsClient) MakeBucket() error { // SetBucketACL - create a new bucket func (f *fsClient) SetBucketACL(acl string) error { if !isValidBucketACL(acl) { - return iodine.New(errors.New("invalid acl"), nil) + return iodine.New(client.InvalidACLType{ACL: acl}, nil) } err := os.MkdirAll(f.path, aclToPerm(acl)) if err != nil { diff --git a/pkg/client/s3/httptrace.go b/pkg/client/s3/httptrace.go index b20344de..5caba4ce 100644 --- a/pkg/client/s3/httptrace.go +++ b/pkg/client/s3/httptrace.go @@ -17,7 +17,6 @@ package s3 import ( - "errors" "net/http" "time" @@ -43,7 +42,7 @@ func (t RoundTripTrace) RoundTrip(req *http.Request) (res *http.Response, err er timeStamp := time.Now() if t.Transport == nil { - return nil, iodine.New(client.InvalidArgument{Err: errors.New("invalid argument")}, nil) + return nil, iodine.New(client.InvalidArgument{}, nil) } res, err = t.Transport.RoundTrip(req) diff --git a/sync-url.go b/sync-url.go index ee72a0c4..d591f46d 100644 --- a/sync-url.go +++ b/sync-url.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "path/filepath" "strings" @@ -123,7 +122,7 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs defer close(syncURLsCh) if !isURLRecursive(sourceURL) { // Source is not of recursive type. - syncURLsCh <- syncURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not recursive.", sourceURL), nil)} + syncURLsCh <- syncURLs{Error: iodine.New(errSourceNotRecursive{URL: sourceURL}, nil)} return } // add `/` after trimming off `...` to emulate directories @@ -143,7 +142,7 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs if !sourceContent.Type.IsDir() { // Source is not a dir. - syncURLsCh <- syncURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not a directory.", sourceURL), nil)} + syncURLsCh <- syncURLs{Error: iodine.New(errSourceIsNotDir{URL: sourceURL}, nil)} return } @@ -158,13 +157,13 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs targetContent, err := targetClient.Stat() if err != nil { // Target does not exist. - syncURLsCh <- syncURLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} + syncURLsCh <- syncURLs{Error: iodine.New(errTargetNotFound{URL: targetURL}, nil)} return } if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - syncURLsCh <- syncURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + syncURLsCh <- syncURLs{Error: iodine.New(errTargetIsNotDir{URL: targetURL}, nil)} return } }