From bb3c89d8446fd82a2b34f6a0443cb3231e7c3571 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Mon, 24 Aug 2015 22:29:50 -0700 Subject: [PATCH] convert remaining typed errors to probe errors --- aliases.go | 2 +- common-methods.go | 4 +- cp-url.go | 18 ++++----- diff.go | 2 +- hostconfig.go | 4 +- mirror-url.go | 2 +- typed-errors.go | 96 ++++++++++++++++++----------------------------- url.go | 2 +- 8 files changed, 54 insertions(+), 76 deletions(-) diff --git a/aliases.go b/aliases.go index 43324eec..22c09499 100644 --- a/aliases.go +++ b/aliases.go @@ -74,7 +74,7 @@ func aliasExpand(aliasedURL string, aliases map[string]string) (string, *probe.E trimmedURL := expandedURL + "/" + strings.TrimPrefix(strings.TrimPrefix(splits[1], "/"), "\\") u, err := client.Parse(trimmedURL) if err != nil { - return aliasedURL, probe.NewError(eInvalidURL{URL: aliasedURL}) + return aliasedURL, errInvalidURL(aliasedURL).Trace() } return u.String(), nil } diff --git a/common-methods.go b/common-methods.go index 9f1bc3f5..a106266b 100644 --- a/common-methods.go +++ b/common-methods.go @@ -165,7 +165,7 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error) case client.Filesystem: return fs.New(urlStr) } - return nil, probe.NewError(eInitClient{URL: urlStr}) + return nil, errInitClient(urlStr).Trace() } // url2Stat - Returns client, config and its stat Content from the URL @@ -199,7 +199,7 @@ func isValidURL(url string) bool { func url2Client(url string) (client.Client, *probe.Error) { if !isValidURL(url) { - return nil, probe.NewError(eInvalidURL{URL: url}) + return nil, errInvalidURL(url).Trace() } urlconfig, err := getHostConfig(url) if err != nil { diff --git a/cp-url.go b/cp-url.go index 6b0bfe00..aa8b85ad 100644 --- a/cp-url.go +++ b/cp-url.go @@ -236,7 +236,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) copyURLs { } if !sourceContent.Type.IsRegular() { // Source is not a regular file - return copyURLs{Error: probe.NewError(eInvalidSource{URL: sourceURL})} + return copyURLs{Error: errInvalidSource(sourceURL).Trace()} } // All OK.. We can proceed. Type A sourceContent.Name = sourceURL @@ -253,19 +253,19 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs { } if !sourceContent.Type.IsRegular() { // Source is not a regular file. - return copyURLs{Error: probe.NewError(eInvalidSource{URL: sourceURL})} + return copyURLs{Error: errInvalidSource(sourceURL).Trace()} } // All OK.. We can proceed. Type B: source is a file, target is a folder and exists. { sourceURLParse, err := client.Parse(sourceURL) if err != nil { - return copyURLs{Error: probe.NewError(eInvalidSource{URL: sourceURL})} + return copyURLs{Error: errInvalidSource(sourceURL).Trace()} } targetURLParse, err := client.Parse(targetURL) if err != nil { - return copyURLs{Error: probe.NewError(eInvalidTarget{URL: targetURL})} + return copyURLs{Error: errInvalidTarget(targetURL).Trace()} } targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) return prepareCopyURLsTypeA(sourceURL, targetURLParse.String()) @@ -280,7 +280,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { defer close(copyURLsCh) if !isURLRecursive(sourceURL) { // Source is not of recursive type. - copyURLsCh <- copyURLs{Error: probe.NewError(eSourceNotRecursive{URL: sourceURL})} + copyURLsCh <- copyURLs{Error: errSourceNotRecursive(sourceURL).Trace()} return } @@ -295,7 +295,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { if !sourceContent.Type.IsDir() { // Source is not a dir. - copyURLsCh <- copyURLs{Error: probe.NewError(eSourceIsNotDir{URL: sourceURL})} + copyURLsCh <- copyURLs{Error: errSourceIsNotDir(sourceURL).Trace()} return } @@ -314,13 +314,13 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { // All OK.. We can proceed. Type B: source is a file, target is a folder and exists. sourceURLParse, err := client.Parse(sourceURL) if err != nil { - copyURLsCh <- copyURLs{Error: probe.NewError(eInvalidSource{URL: sourceURL})} + copyURLsCh <- copyURLs{Error: errInvalidSource(sourceURL).Trace()} continue } targetURLParse, err := client.Parse(targetURL) if err != nil { - copyURLsCh <- copyURLs{Error: probe.NewError(eInvalidTarget{URL: targetURL})} + copyURLsCh <- copyURLs{Error: errInvalidTarget(targetURL).Trace()} continue } @@ -330,7 +330,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { sourceContentURL := sourceURLDelimited + sourceContentName sourceContentParse, err := client.Parse(sourceContentURL) if err != nil { - copyURLsCh <- copyURLs{Error: probe.NewError(eInvalidSource{URL: sourceContentName})} + copyURLsCh <- copyURLs{Error: errInvalidSource(sourceContentName).Trace()} continue } diff --git a/diff.go b/diff.go index c449d61b..04275fe7 100644 --- a/diff.go +++ b/diff.go @@ -172,7 +172,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan DiffMessage) { } default: ch <- DiffMessage{ - Error: probe.NewError(eNotAnObject{URL: firstURL}), + Error: errNotAnObject(firstURL).Trace(), } return } diff --git a/hostconfig.go b/hostconfig.go index d6d744c5..d1808676 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -50,12 +50,12 @@ func getHostConfig(URL string) (hostConfig, *probe.Error) { for globURL, hostCfg := range config.Hosts { match, err := filepath.Match(globURL, url.Host) if err != nil { - return hostConfig{}, probe.NewError(eInvalidGlobURL{glob: globURL, request: URL}) + return hostConfig{}, errInvalidGlobURL(globURL, URL).Trace() } if match { return hostCfg, nil } } } - return hostConfig{}, probe.NewError(eNoMatchingHost{URL: URL}) + return hostConfig{}, errNoMatchingHost(URL).Trace() } diff --git a/mirror-url.go b/mirror-url.go index f4c5c76a..0a667d65 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -197,7 +197,7 @@ func prepareMirrorURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs } // if one of the targets is not dir exit if !targetContent.Type.IsDir() { - mirrorURLsCh <- mirrorURLs{Error: probe.NewError(eInvalidTarget{URL: targetURL})} + mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(targetURL).Trace()} return } // special case, be extremely careful before changing this behavior - will lead to data loss diff --git a/typed-errors.go b/typed-errors.go index ea19ba96..c77ea769 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -26,69 +26,47 @@ var ( errDummy = func() *probe.Error { return probe.NewError(errors.New("")).Untrace() } + errInvalidArgument = func() *probe.Error { return probe.NewError(errors.New("Invalid arguments provided, cannot proceed.")).Untrace() } + errSourceListEmpty = func() *probe.Error { return probe.NewError(errors.New("Source argument list is empty.")).Untrace() } + + errInvalidGlobURL = func(glob, request string) *probe.Error { + return probe.NewError(errors.New("Error reading glob URL ‘" + glob + "’ while comparing with ‘" + request + "’.")).Untrace() + } + + errInvalidURL = func(URL string) *probe.Error { + return probe.NewError(errors.New("Invalid URL ‘" + URL + "’.")).Untrace() + } + + errNoMatchingHost = func(URL string) *probe.Error { + return probe.NewError(errors.New("No matching host found for the given URL ‘" + URL + "’.")).Untrace() + } + + errInitClient = func(URL string) *probe.Error { + return probe.NewError(errors.New("Unable to initialize client for URL ‘" + URL + "’.")).Untrace() + } + + errInvalidSource = func(URL string) *probe.Error { + return probe.NewError(errors.New("Invalid source ‘" + URL + "’.")).Untrace() + } + + errInvalidTarget = func(URL string) *probe.Error { + return probe.NewError(errors.New("Invalid target ‘" + URL + "’.")).Untrace() + } + + errSourceNotRecursive = func(URL string) *probe.Error { + return probe.NewError(errors.New("Source ‘" + URL + "’ is not recursive.")).Untrace() + } + + errSourceIsNotDir = func(URL string) *probe.Error { + return probe.NewError(errors.New("Source ‘" + URL + "’ is not a folder.")).Untrace() + } + errNotAnObject = func(URL string) *probe.Error { + return probe.NewError(errors.New("‘" + URL + "’ is not an object.")).Untrace() + } ) - -type eInvalidGlobURL struct { - glob string - request string -} - -func (e eInvalidGlobURL) Error() string { - return "Error reading glob URL ‘" + e.glob + "’ while comparing with ‘" + e.request + "’." -} - -type eInvalidURL struct { - URL string -} - -func (e eInvalidURL) Error() string { - return "Invalid URL " + e.URL -} - -type eNoMatchingHost eInvalidURL - -func (e eNoMatchingHost) Error() string { - return "No matching host found for the given URL ‘" + e.URL + "’." -} - -type eInitClient eInvalidURL - -func (e eInitClient) Error() string { - return "Unable to initialize client for URL ‘" + e.URL + "’." -} - -type eInvalidSource eInvalidURL - -func (e eInvalidSource) Error() string { - return "Invalid source " + e.URL -} - -type eInvalidTarget eInvalidURL - -func (e eInvalidTarget) Error() string { - return "Invalid target " + e.URL -} - -type eSourceNotRecursive eInvalidURL - -func (e eSourceNotRecursive) Error() string { - return "Source ‘" + e.URL + "’ is not recursive." -} - -type eSourceIsNotDir eInvalidURL - -func (e eSourceIsNotDir) Error() string { - return "Source ‘" + e.URL + "’ is not a folder." -} - -type eNotAnObject eInvalidURL - -func (e eNotAnObject) Error() string { - return "‘" + e.URL + "’ is not an object." -} diff --git a/url.go b/url.go index 0d29b54d..8f435cd1 100644 --- a/url.go +++ b/url.go @@ -54,7 +54,7 @@ func getCanonicalizedURL(arg string, aliases map[string]string) (urlStr string, } if _, err := client.Parse(urlStr); err != nil { // Not a valid URL. Return error - return "", probe.NewError(eInvalidURL{urlStr}) + return "", errInvalidURL(urlStr).Trace() } return urlStr, nil }