From e003a242f63ca5347f126abf5292dcb16323f93d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 3 Aug 2015 20:58:00 -0700 Subject: [PATCH] Handle errors better with new changes, changes across cp, mirror, mb --- cp-url.go | 70 ++++++++++++++++------------------------------- errors.go | 6 ++++ ls-main.go | 3 +- mb-main.go | 17 ++++-------- mb_access_test.go | 27 +++++++++--------- mirror-url.go | 16 ++++------- 6 files changed, 56 insertions(+), 83 deletions(-) diff --git a/cp-url.go b/cp-url.go index 16420d61..b9687cb1 100644 --- a/cp-url.go +++ b/cp-url.go @@ -108,49 +108,35 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace()) - } + ifFatal(err) if srcContent.Type.IsDir() { - console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, probe.New(errInvalidArgument{})) + console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{})) - } - - _, tgtContent, err := url2Stat(tgtURL) - // Target exist?. - if err == nil { - if !tgtContent.Type.IsRegular() { - console.Fatalf("Target ‘%s’ is not a file. %s\n", tgtURL, probe.New(errInvalidArgument{})) - } + ifFatal(probe.New(errSourceIsNotFile{URL: srcURL})) } } // checkCopySyntaxTypeB verifies if the source is a valid file and target is a valid dir. func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { - console.Fatalf("Invalid number of source arguments to copy command. %s\n", probe.New(errInvalidArgument{})) + console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument{}) } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, probe.New(err)) - } + ifFatal(err) if srcContent.Type.IsDir() { - console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, probe.New(errInvalidArgument{})) + console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{})) + ifFatal(probe.New(errSourceIsNotFile{URL: srcURL})) } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{})) + ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL})) } } } @@ -158,24 +144,21 @@ func checkCopySyntaxTypeB(srcURLs []string, tgtURL string) { // checkCopySyntaxTypeC verifies if the source is a valid recursive dir and target is a valid dir. func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { - console.Fatalf("Invalid number of source arguments to copy command. %s\n", probe.New(errInvalidArgument{})) + console.Fatalf("Invalid number of source arguments to copy command. %s\n", errInvalidArgument{}) } srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace()) - } - if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errInvalidArgument{})) - } + ifFatal(err) + if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. + ifFatal(probe.New(errSourceIsNotDir{URL: srcURL})) + } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. if err == nil { if !tgtContent.Type.IsDir() { - console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{})) + ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL})) } } } @@ -186,34 +169,27 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { if isURLRecursive(srcURL) { srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace()) - } + ifFatal(err) if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. - console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errInvalidArgument{})) + ifFatal(probe.New(errSourceIsNotDir{URL: srcURL})) } } else { // Regular URL. _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace()) - } + ifFatal(err) if srcContent.Type.IsDir() { - console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, probe.New(errInvalidArgument{})) + console.Fatalf("Source ‘%s’ is a folder. Use ‘%s...’ argument to copy this folder and its contents recursively. %s\n", srcURL, srcURL, errInvalidArgument{}) } if !srcContent.Type.IsRegular() { - console.Fatalf("Source ‘%s’ is not a file. %s\n", srcURL, probe.New(errInvalidArgument{})) + ifFatal(probe.New(errSourceIsNotFile{URL: srcURL})) } } } _, tgtContent, err := url2Stat(tgtURL) // Target exist?. - if err != nil { - console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, err.Trace()) - } - if !tgtContent.Type.IsDir() { - console.Fatalf("Target ‘%s’ is not a folder. %s\n", tgtURL, probe.New(errInvalidArgument{})) + if err == nil { + if !tgtContent.Type.IsDir() { + ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL})) + } } } diff --git a/errors.go b/errors.go index 1f46cc68..403e9fdd 100644 --- a/errors.go +++ b/errors.go @@ -161,6 +161,12 @@ func (e errSourceIsNotDir) Error() string { return "Source ‘" + e.URL + "’ is not a folder." } +type errSourceIsNotFile errTargetIsNotDir + +func (e errSourceIsNotFile) Error() string { + return "Source ‘" + e.URL + "’ is not a file." +} + type errSourceListEmpty errInvalidArgument func (e errSourceListEmpty) Error() string { diff --git a/ls-main.go b/ls-main.go index c00db5be..7b37fc70 100644 --- a/ls-main.go +++ b/ls-main.go @@ -86,8 +86,7 @@ func runListCmd(ctx *cli.Context) { ifFatal(err) // if recursive strip off the "..." newTargetURL := stripRecursiveURL(targetURL) - err = doListCmd(newTargetURL, isURLRecursive(targetURL)) - ifFatal(err) + ifFatal(doListCmd(newTargetURL, isURLRecursive(targetURL))) } } diff --git a/mb-main.go b/mb-main.go index e0e4af45..9605a23b 100644 --- a/mb-main.go +++ b/mb-main.go @@ -17,8 +17,6 @@ package main import ( - "fmt" - "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/probe" @@ -63,23 +61,20 @@ func runMakeBucketCmd(ctx *cli.Context) { for _, arg := range ctx.Args() { targetURL, err := getExpandedURL(arg, config.Aliases) ifFatal(err) - msg, err := doMakeBucketCmd(targetURL) - ifFatal(err) - console.Infoln(msg) + ifFatal(doMakeBucketCmd(targetURL)) + console.Infoln("Bucket created successfully : " + targetURL) } } // doMakeBucketCmd - -func doMakeBucketCmd(targetURL string) (string, *probe.Error) { +func doMakeBucketCmd(targetURL string) *probe.Error { clnt, err := target2Client(targetURL) if err != nil { - msg := fmt.Sprintf("Unable to initialize client for ‘%s’", targetURL) - return msg, err.Trace() + return err.Trace() } err = clnt.MakeBucket() if err != nil { - msg := fmt.Sprintf("Failed to create bucket for URL ‘%s’", clnt.URL().String()) - return msg, err.Trace() + return err.Trace() } - return "Bucket created successfully : " + clnt.URL().String(), nil + return nil } diff --git a/mb_access_test.go b/mb_access_test.go index 6d68a213..d651c64b 100644 --- a/mb_access_test.go +++ b/mb_access_test.go @@ -30,24 +30,25 @@ func (s *CmdTestSuite) TestMbAndAccessCmd(c *C) { root, err := ioutil.TempDir(os.TempDir(), "cmd-") c.Assert(err, IsNil) defer os.RemoveAll(root) + { + err := doMakeBucketCmd(filepath.Join(root, "bucket")) + c.Assert(err, IsNil) - _, err = doMakeBucketCmd(filepath.Join(root, "bucket")) - c.Assert(err, IsNil) + err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write") + c.Assert(err, IsNil) - err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write") - c.Assert(err, IsNil) + err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid") + c.Assert(err, Not(IsNil)) - err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid") - c.Assert(err, Not(IsNil)) + err = doMakeBucketCmd(server.URL + "/bucket") + c.Assert(err, IsNil) - _, err = doMakeBucketCmd(server.URL + "/bucket") - c.Assert(err, IsNil) + err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write") + c.Assert(err, IsNil) - err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write") - c.Assert(err, IsNil) - - err = doUpdateAccessCmd(server.URL+"/bucket", "invalid") - c.Assert(err, Not(IsNil)) + err = doUpdateAccessCmd(server.URL+"/bucket", "invalid") + c.Assert(err, Not(IsNil)) + } } diff --git a/mirror-url.go b/mirror-url.go index 09324d54..ce88564a 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -57,9 +57,7 @@ func checkMirrorSyntax(ctx *cli.Context) { // extract URLs. URLs, err := args2URLs(ctx.Args()) - if err != nil { - console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err.Trace()) - } + ifFatal(err) srcURL := URLs[0] tgtURLs := URLs[1:] @@ -68,10 +66,8 @@ func checkMirrorSyntax(ctx *cli.Context) { // Source cannot be a folder (except when recursive) if !isURLRecursive(srcURL) { _, srcContent, err := url2Stat(srcURL) - // Source exist?. - if err != nil { - console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, err.Trace()) - } + ifFatal(err) + if !srcContent.Type.IsRegular() { if srcContent.Type.IsDir() { console.Fatalf("Source ‘%s’ is a folder. Please use ‘%s...’ to recursively copy this folder and its contents.\n", srcURL, srcURL) @@ -93,20 +89,20 @@ func checkMirrorSyntax(ctx *cli.Context) { } if len(tgtURLs) == 0 && tgtURLs == nil { - console.Fatalf("Invalid number of target arguments to mirror command. %s\n", probe.New(errInvalidArgument{})) + console.Fatalf("Invalid number of target arguments to mirror command. %s\n", errInvalidArgument{}) } srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) ifFatal(err) if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. - console.Fatalf("Source ‘%s’ is not a folder. %s\n", stripRecursiveURL(srcURL), probe.New(errSourceIsNotDir{})) + ifFatal(probe.New(errSourceIsNotDir{URL: srcURL})) } for _, tgtURL := range tgtURLs { _, content, err := url2Stat(tgtURL) if err == nil { if !content.Type.IsDir() { - console.Fatalf("One of the target ‘%s’ is not a folder. cannot have mixtures of directories and files while copying directories recursively. %s\n", tgtURL, probe.New(errInvalidArgument{})) + ifFatal(probe.New(errTargetIsNotDir{URL: tgtURL})) } } }