Improve errors strictly type them, first cut #286

This commit is contained in:
Harshavardhana
2015-04-11 21:48:00 -07:00
parent d42d70bee3
commit 0508022597
4 changed files with 30 additions and 22 deletions
+1 -1
View File
@@ -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)
}
}
+1 -1
View File
@@ -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)
+25 -17
View File
@@ -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
}
+3 -3
View File
@@ -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) {