From 940fd7c608713fc204734227045336a71eee7335 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 13 Nov 2015 00:13:11 -0800 Subject: [PATCH] error-handling: Add proper typed errors and converged code url2Content. --- cp-url.go | 18 +++---------- ls.go | 38 ++++++-------------------- mirror-url.go | 17 +----------- pkg/client/errors.go | 64 +++++++++++--------------------------------- pkg/client/fs/fs.go | 26 +++++++++++++----- pkg/client/s3/s3.go | 1 + url.go | 21 +++++++++++++++ 7 files changed, 69 insertions(+), 116 deletions(-) diff --git a/cp-url.go b/cp-url.go index 04497a3b..7dd279ad 100644 --- a/cp-url.go +++ b/cp-url.go @@ -143,23 +143,11 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { if len(srcURLs) != 1 { fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments to copy command.") } + srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) - _, srcContent, err := url2Stat(srcURL) - if err != nil { - // if file not found it could be that user has provided a valid - // prefix, populate parent content properly and set error to nil. - // if at all the prefix doesn't exist eventually 'List' will handle - // it properly. - if os.IsNotExist(err.ToGoError()) { - // set the err back to nil consciously. - err = nil - // fill in srcContent for subsequent verification. - srcContent = new(client.Content) - srcContent.URL = *client.NewURL(srcURL) - srcContent.Type = os.ModeDir - } - } + + srcContent, err := url2Content(srcURL) fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. diff --git a/ls.go b/ls.go index 374d5af9..af89d4b4 100644 --- a/ls.go +++ b/ls.go @@ -19,7 +19,6 @@ package main import ( "encoding/json" "fmt" - "os" "runtime" "strings" "time" @@ -140,22 +139,9 @@ func trimContent(parentContent, childContent *client.Content, recursive bool) *c // doList - list all entities inside a folder. func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { - _, parentContent, err := url2Stat(clnt.GetURL().String()) + parentContent, err := url2Content(clnt.GetURL().String()) if err != nil { - // if file not found it could be that user has provided a valid - // prefix, populate parent content properly and set error to nil. - // if at all the prefix doesn't exist eventually 'List' will handle - // it properly. - if os.IsNotExist(err.ToGoError()) { - // set the err back to nil consciously. - err = nil - // fill parentContent with input client - parentContent = new(client.Content) - parentContent.URL = clnt.GetURL() - parentContent.Type = os.ModeDir - } else { - return err.Trace(clnt.GetURL().String()) - } + return err.Trace(clnt.GetURL().String()) } for contentCh := range clnt.List(isRecursive, isIncomplete) { if contentCh.Err != nil { @@ -167,20 +153,12 @@ func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { case client.TooManyLevelsSymlink: errorIf(contentCh.Err.Trace(), "Unable to list too many levels link.") continue - } - if os.IsNotExist(contentCh.Err.ToGoError()) || os.IsPermission(contentCh.Err.ToGoError()) { - if contentCh.Content != nil { - if contentCh.Content.Type.IsDir() { - if contentCh.Content.Type&os.ModeSymlink == os.ModeSymlink { - errorIf(contentCh.Err.Trace(), "Unable to list broken folder link.") - continue - } - errorIf(contentCh.Err.Trace(), "Unable to list folder.") - } - } else { - errorIf(contentCh.Err.Trace(), "Unable to list.") - continue - } + case client.PathNotFound: + errorIf(contentCh.Err.Trace(), "Unable to list missing broken folder link.") + continue + case client.PathInsufficientPermission: + errorIf(contentCh.Err.Trace(), "Unable to list folder.") + continue } err = contentCh.Err.Trace() break diff --git a/mirror-url.go b/mirror-url.go index 4e61886b..5397581e 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "os" "strings" "github.com/minio/cli" @@ -63,21 +62,7 @@ func checkMirrorSyntax(ctx *cli.Context) { /****** Generic rules *******/ // Recursive source URL. newSrcURL := stripRecursiveURL(srcURL) - _, srcContent, err := url2Stat(newSrcURL) - if err != nil { - // if file not found it could be that user has provided a valid - // prefix, populate parent content properly and set error to nil. - // if at all the prefix doesn't exist eventually 'List' will handle - // it properly. - if os.IsNotExist(err.ToGoError()) { - // set the err back to nil consciously. - err = nil - // fill srcContent for subsequent verification. - srcContent = new(client.Content) - srcContent.URL = *client.NewURL(newSrcURL) - srcContent.Type = os.ModeDir - } - } + srcContent, err := url2Content(newSrcURL) fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+newSrcURL+"’.") if !srcContent.Type.IsDir() { diff --git a/pkg/client/errors.go b/pkg/client/errors.go index e73e23af..083c2855 100644 --- a/pkg/client/errors.go +++ b/pkg/client/errors.go @@ -30,45 +30,13 @@ func (e APINotImplemented) Error() string { return "‘" + e.API + "’ feature " + "is not implemented for ‘" + e.APIType + "’" } -// UnexpectedError - unexpected error -type UnexpectedError GenericError - -func (e UnexpectedError) Error() string { - 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 "Invalid argument" -} - // InvalidRange - invalid range requested type InvalidRange struct { Offset int64 } 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 -} - -// InvalidMaxKeys - invalid maxkeys provided -type InvalidMaxKeys struct { - MaxKeys int -} - -func (e InvalidMaxKeys) Error() string { - return "invalid maxkeys: " + strconv.Itoa(e.MaxKeys) + return "Invalid range offset: " + strconv.FormatInt(e.Offset, 10) } // GenericBucketError - generic bucket operations error @@ -80,7 +48,7 @@ type GenericBucketError struct { type BucketExists GenericBucketError func (e BucketExists) Error() string { - return "bucket " + e.Bucket + " exists" + return "Bucket #" + e.Bucket + " exists" } // InvalidBucketName - bucket name invalid (http://goo.gl/wJlzDz) @@ -105,25 +73,18 @@ func (e ObjectAlreadyExists) Error() string { return "Object #" + e.Object + " already exists." } -// ObjectNotFound - object requested does not exist -type ObjectNotFound GenericObjectError - -func (e ObjectNotFound) Error() string { - return "object " + e.Object + " not found in bucket " + e.Bucket -} - // InvalidObjectName - object requested is invalid type InvalidObjectName GenericObjectError func (e InvalidObjectName) Error() string { - return "object " + e.Object + "at" + e.Bucket + "is invalid" + return "Object #" + e.Object + " at " + e.Bucket + " is invalid" } // ObjectExists - object exists type ObjectExists GenericObjectError func (e ObjectExists) Error() string { - return "object " + e.Object + " exists" + return "Object #" + e.Object + " exists" } // GenericError - generic error @@ -138,19 +99,26 @@ func (e InvalidQueryURL) Error() string { return "Invalid query URL: " + e.URL } -// GenericFileError - generic file error +// GenericFileError - generic file error. type GenericFileError struct { Path string } -// NotFound (ENOENT) - file not found -type NotFound GenericFileError +// PathNotFound (ENOENT) - file not found. +type PathNotFound GenericFileError -func (e NotFound) Error() string { +func (e PathNotFound) Error() string { return "Requested file ‘" + e.Path + "’ not found" } -// BrokenSymlink (ENOTENT) - file has broken symlink +// PathInsufficientPermission (EPERM) - permission denied. +type PathInsufficientPermission GenericFileError + +func (e PathInsufficientPermission) Error() string { + return "Insufficient permissions to access this file ‘" + e.Path + "’" +} + +// BrokenSymlink (ENOTENT) - file has broken symlink. type BrokenSymlink GenericFileError func (e BrokenSymlink) Error() string { diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 92c3ab6b..bdf31ee3 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -75,6 +75,12 @@ func (f *fsClient) fsStat() (os.FileInfo, *probe.Error) { } } if err != nil { + if os.IsNotExist(err) { + return nil, probe.NewError(client.PathNotFound{Path: fpath}) + } + if os.IsPermission(err) { + return nil, probe.NewError(client.PathInsufficientPermission{Path: fpath}) + } return nil, probe.NewError(err) } st, err := os.Stat(fpath) @@ -91,10 +97,13 @@ func (f *fsClient) fsStat() (os.FileInfo, *probe.Error) { return lst, nil } } - if os.IsNotExist(err) { - return nil, probe.NewError(client.NotFound{Path: fpath}) - } if err != nil { + if os.IsNotExist(err) { + return nil, probe.NewError(client.PathNotFound{Path: fpath}) + } + if os.IsPermission(err) { + return nil, probe.NewError(client.PathInsufficientPermission{Path: fpath}) + } return nil, probe.NewError(err) } return st, nil @@ -170,10 +179,13 @@ func (f *fsClient) Get(offset, length int64) (io.ReadCloser, int64, *probe.Error // Resolve symlinks _, err := filepath.EvalSymlinks(tmppath) - if os.IsNotExist(err) { - return nil, length, probe.NewError(err) - } if err != nil { + if os.IsNotExist(err) { + return nil, length, probe.NewError(client.PathNotFound{Path: tmppath}) + } + if os.IsPermission(err) { + return nil, length, probe.NewError(client.PathInsufficientPermission{Path: tmppath}) + } return nil, length, probe.NewError(err) } if offset == 0 && length == 0 { @@ -466,7 +478,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } else { contentCh <- client.ContentOnChannel{ Content: nil, - Err: probe.NewError(err), + Err: probe.NewError(client.PathInsufficientPermission{Path: fp}), } } return nil diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index d353b876..29d35aa3 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -239,6 +239,7 @@ func (c *s3Client) Stat() (*client.Content, *probe.Error) { content.Content.Size = 0 return content.Content, nil } + return nil, probe.NewError(client.PathNotFound{Path: c.hostURL.Path}) } } return nil, probe.NewError(err) diff --git a/url.go b/url.go index beaf8600..95ff40a2 100644 --- a/url.go +++ b/url.go @@ -17,6 +17,7 @@ package main import ( + "os" "path/filepath" "strings" @@ -99,3 +100,23 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err } return client, content, nil } + +// url2Content returns content info for URL. +func url2Content(urlStr string) (content *client.Content, err *probe.Error) { + _, content, err = url2Stat(urlStr) + if err == nil { + return + } + switch err.ToGoError().(type) { + // if file not found it could be that user has provided a valid + // prefix, populate parent content properly and set error to nil. + // if at all the prefix doesn't exist eventually 'List' will handle it properly. + case client.PathNotFound: + // fill content with input url + content = new(client.Content) + content.URL = *client.NewURL(urlStr) + content.Type = os.ModeDir + return content, nil + } + return nil, err.Trace(urlStr) +}