From 0508022597261e50dbbfd4f8f79a1f1cc2b6c5ca Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 11 Apr 2015 21:46:50 -0700 Subject: [PATCH] Improve errors strictly type them, first cut #286 --- cmd-common.go | 2 +- cmd-mb.go | 2 +- errors.go | 42 +++++++++++++++++++++++++----------------- url-parser.go | 6 +++--- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cmd-common.go b/cmd-common.go index d1c86195..831d45e0 100644 --- a/cmd-common.go +++ b/cmd-common.go @@ -79,6 +79,6 @@ func getNewClient(debug bool, urlStr string) (clnt client.Client, err error) { clnt = fs.GetNewClient(urlStr) return clnt, nil default: - return nil, iodine.New(errUnsupportedScheme, nil) + return nil, iodine.New(errUnsupportedScheme{scheme: uType}, nil) } } diff --git a/cmd-mb.go b/cmd-mb.go index b106bbcd..2b6e5ad5 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -55,7 +55,7 @@ func doMakeBucketCmd(ctx *cli.Context) { if !strings.HasPrefix(urlStr, "file:") { if !client.IsValidBucketName(bucket) { log.Debug.Println(iodine.New(err, nil)) - console.Errorln(errInvalidbucket) + console.Errorln(errInvalidBucket{bucket: bucket}) } } err = clnt.PutBucket(bucket) diff --git a/errors.go b/errors.go index aecc505b..67e8f4f2 100644 --- a/errors.go +++ b/errors.go @@ -16,26 +16,34 @@ package main -import "errors" +import "strconv" -var ( - // TODO remove, unused - // fs - // errFskey = errors.New("Key is needed to get the file") +type errEmptyURL struct{} - // url - errEmptyURL = errors.New("URL is empty") +func (e errEmptyURL) Error() string { + return "URL is empty" +} - // url - errUnsupportedScheme = errors.New("Unsupported URL scheme") +type errUnsupportedScheme struct { + scheme urlType +} - // url - errInvalidURL = errors.New("Invalid URL") +func (e errUnsupportedScheme) Error() string { + return "Unsuppported URL scheme: " + strconv.Itoa(int(e.scheme)) +} - // TODO remove, unused - // host - // errHostname = errors.New("No hostname specified") +type errInvalidURL struct { + url string +} - // common - errInvalidbucket = errors.New("Invalid bucket name") -) +func (e errInvalidURL) Error() string { + return "Invalid URL: " + e.url +} + +type errInvalidBucket struct { + bucket string +} + +func (e errInvalidBucket) Error() string { + return "Invalid bucket name: " + e.bucket +} diff --git a/url-parser.go b/url-parser.go index 70f65692..51c8f3e0 100644 --- a/url-parser.go +++ b/url-parser.go @@ -87,7 +87,7 @@ func isValidFileURL(urlStr string) bool { // fixFileURL rewrites file URL to proper file:///path/to/ form. func fixFileURL(urlStr string) (fixedURL string, err error) { if urlStr == "" { - return "", iodine.New(errEmptyURL, nil) + return "", iodine.New(errEmptyURL{}, nil) } utype, e := getURLType(urlStr) if e != nil || utype != urlFile { @@ -102,7 +102,7 @@ func fixFileURL(urlStr string) (fixedURL string, err error) { // file:///path should always have empty host if u.Host != "" { // Not really a file URL. Host is not empty. - return "", iodine.New(errInvalidURL, nil) + return "", iodine.New(errInvalidURL{url: urlStr}, nil) } // do not use u.Scheme since that would construct a path in the form // file:// which is an invalid file but url Parse doesn't report error @@ -174,7 +174,7 @@ func parseURL(arg string, aliases map[string]string) (urlStr string, err error) } if !isValidURL(urlStr) { - return "", iodine.New(errUnsupportedScheme, nil) + return "", iodine.New(errUnsupportedScheme{scheme: urlUnknown}, nil) } // If it is a file URL, rewrite to file:///path/to form if isValidFileURL(urlStr) {