Move from fmt.Errorf to errors.New

In many cases fmt.Errorf is good enough, but since
error is an interface, you can use arbitrary data
structures as error values, to allow callers to
inspect the details of the error.
This commit is contained in:
Harshavardhana
2015-03-15 19:52:35 -07:00
parent 385abe1bef
commit 537fa331ba
4 changed files with 21 additions and 13 deletions
+7 -4
View File
@@ -17,6 +17,7 @@
package main
import (
"errors"
"fmt"
"io"
"os"
@@ -63,14 +64,15 @@ func getMode(recursive bool, args *cmdArgs) int {
// First mode <Object> <S3Object> or <Object> <S3Bucket>
func firstMode(s3c *s3.Client, args *cmdArgs) error {
if args.source.key == "" {
return fmt.Errorf("invalid args")
return errors.New("invalid args")
}
st, err := os.Stat(args.source.key)
if os.IsNotExist(err) {
return err
}
if st.IsDir() {
return fmt.Errorf("omitting directory '%s'", st.Name())
msg := fmt.Sprintf("omitting directory '%s'", st.Name())
return errors.New(msg)
}
size := st.Size()
source, err := os.Open(args.source.key)
@@ -133,7 +135,8 @@ func secondMode(s3c *s3.Client, args *cmdArgs) error {
downloadedSize = st.Size()
// Verify if file is already downloaded
if downloadedSize == objectSize {
return fmt.Errorf("%s object has been already downloaded", args.destination.key)
msg := fmt.Sprintf("%s object has been already downloaded", args.destination.key)
return errors.New(msg)
}
destination, err = os.OpenFile(args.destination.key, os.O_RDWR, 0600)
@@ -214,7 +217,7 @@ func thirdMode(s3c *s3.Client, args *cmdArgs) error {
return err
}
case false:
return fmt.Errorf("Ranges not supported")
return errors.New("Ranges not supported")
}
msg := fmt.Sprintf("http://%s/%s uploaded -- to bucket:(http://%s/%s)", args.source.bucket, args.source.key,
+4 -3
View File
@@ -17,6 +17,7 @@
package main
import (
"errors"
"fmt"
"os"
"path"
@@ -65,10 +66,10 @@ func (w *walk) putWalk(p string, i os.FileInfo, err error) error {
func isValidBucketName(p string) error {
if path.IsAbs(p) {
return fmt.Errorf("directory bucketname cannot be absolute")
return errors.New("directory bucketname cannot be absolute")
}
if strings.HasPrefix(p, "..") {
return fmt.Errorf("Relative directory references not supported")
return errors.New("Relative directory references not supported")
}
if !s3.IsValidBucket(p) {
return errInvalidbucket
@@ -101,7 +102,7 @@ func doRecursiveCp(s3c *s3.Client, args *cmdArgs) error {
return err
}
if !st.IsDir() {
return fmt.Errorf("Should be a directory")
return errors.New("Should be a directory")
}
s3c.Host = args.destination.host
+7 -4
View File
@@ -40,6 +40,7 @@ package s3
import (
"bytes"
"errors"
"fmt"
"io"
"sort"
@@ -112,7 +113,7 @@ func (c *Client) ListObjects(bucket string, startAt, prefix, delimiter string, m
var buffer bytes.Buffer
if maxKeys <= 0 {
return nil, nil, fmt.Errorf("negative maxKeys are invalid")
return nil, nil, errors.New("negative maxKeys are invalid")
}
marker := startAt
@@ -162,7 +163,8 @@ func (c *Client) ListObjects(bucket string, startAt, prefix, delimiter string, m
if err != nil {
fmt.Printf("Error parsing s3 XML response: %v for %q", err, logbuf.Bytes())
} else if bres.MaxKeys != fetchN || bres.Name != bucket || bres.Marker != marker {
err = fmt.Errorf("Unexpected parse from server: %#v from: %s", bres, logbuf.Bytes())
msg := fmt.Sprintf("Unexpected parse from server: %#v from: %s", bres, logbuf.Bytes())
err = errors.New(msg)
fmt.Print(err)
}
if err != nil {
@@ -180,7 +182,8 @@ func (c *Client) ListObjects(bucket string, startAt, prefix, delimiter string, m
continue
}
if it.Key < startAt {
return nil, nil, fmt.Errorf("Unexpected response from Amazon: item key %q but wanted greater than %q", it.Key, startAt)
msg := fmt.Sprintf("Unexpected response from Amazon: item key %q but wanted greater than %q", it.Key, startAt)
return nil, nil, errors.New(msg)
}
items = append(items, it)
marker = it.Key
@@ -197,7 +200,7 @@ func (c *Client) ListObjects(bucket string, startAt, prefix, delimiter string, m
}
if len(items) == 0 {
return nil, nil, fmt.Errorf("No items replied")
return nil, nil, errors.New("No items replied")
}
}
sort.Sort(bySize(items))
+3 -2
View File
@@ -40,6 +40,7 @@ package s3
import (
"bytes"
"errors"
"fmt"
"io"
"os"
@@ -68,7 +69,7 @@ func (c *Client) Put(bucket, key string, size int64, contents io.Reader) error {
mw := io.MultiWriter(h, sink)
written, err := io.Copy(mw, contents)
if written != size {
return fmt.Errorf("Data read mismatch")
return errors.New("Data read mismatch")
}
if err != nil {
return err
@@ -143,7 +144,7 @@ func (c *Client) Get(bucket, key string) (body io.ReadCloser, size int64, err er
// If length is negative, the rest of the object is returned.
func (c *Client) GetPartial(bucket, key string, offset, length int64) (body io.ReadCloser, size int64, err error) {
if offset < 0 {
return nil, 0, fmt.Errorf("invalid negative length")
return nil, 0, errors.New("invalid negative length")
}
req := newReq(c.keyURL(bucket, key))