From 596eeb097e425eab1b4d64f077ba8c8390fb44db Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 15 Mar 2015 19:40:30 -0700 Subject: [PATCH] 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. --- pkg/api/minioapi/range.go | 19 ++++++++++--------- pkg/storage/storage_date.go | 7 ++++--- pkg/utils/crypto/signers/signers.go | 5 +++-- pkg/utils/crypto/x509/generator.go | 22 ++++++++++++---------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/pkg/api/minioapi/range.go b/pkg/api/minioapi/range.go index 885090bee..0c4f3793b 100644 --- a/pkg/api/minioapi/range.go +++ b/pkg/api/minioapi/range.go @@ -17,6 +17,7 @@ package minioapi import ( + "errors" "fmt" "strconv" "strings" @@ -58,28 +59,28 @@ func newRange(req *http.Request, size int64) (*httpRange, error) { // parseRange parses a Range header string as per RFC 2616. func (r *httpRange) parseRange(s string) error { if s == "" { - return fmt.Errorf("header not present") + return errors.New("header not present") } if !strings.HasPrefix(s, b) { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } ras := strings.Split(s[len(b):], ",") if len(ras) == 0 { - return fmt.Errorf("invalid request") + return errors.New("invalid request") } // Just pick the first one and ignore the rest, we only support one range per object if len(ras) > 1 { - return fmt.Errorf("multiple ranges specified") + return errors.New("multiple ranges specified") } ra := strings.TrimSpace(ras[0]) if ra == "" { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } i := strings.Index(ra, "-") if i < 0 { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:]) if start == "" { @@ -87,7 +88,7 @@ func (r *httpRange) parseRange(s string) error { // range start relative to the end of the file. i, err := strconv.ParseInt(end, 10, 64) if err != nil { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } if i > r.size { i = r.size @@ -97,7 +98,7 @@ func (r *httpRange) parseRange(s string) error { } else { i, err := strconv.ParseInt(start, 10, 64) if err != nil || i > r.size || i < 0 { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } r.start = i if end == "" { @@ -106,7 +107,7 @@ func (r *httpRange) parseRange(s string) error { } else { i, err := strconv.ParseInt(end, 10, 64) if err != nil || r.start > i { - return fmt.Errorf("invalid range") + return errors.New("invalid range") } if i >= r.size { i = r.size - 1 diff --git a/pkg/storage/storage_date.go b/pkg/storage/storage_date.go index 7c6d31ffc..9ee6e55fc 100644 --- a/pkg/storage/storage_date.go +++ b/pkg/storage/storage_date.go @@ -1,6 +1,7 @@ package storage import ( + "errors" "fmt" "strconv" "strings" @@ -34,7 +35,7 @@ func parseDate(str string) (d Date, err error) { y, m, n int ) if len(str) != 10 || str[4] != '-' || str[7] != '-' { - err = fmt.Errorf("Invalid 0000-00-000 style DATE string: " + str) + err = errors.New("Invalid 0000-00-000 style DATE string: " + str) return } if y, err = strconv.Atoi(str[0:4]); err != nil { @@ -44,14 +45,14 @@ func parseDate(str string) (d Date, err error) { return } if m < 1 || m > 12 { - err = fmt.Errorf("Invalid 0000-00-000 style DATE string: " + str) + err = errors.New("Invalid 0000-00-000 style DATE string: " + str) return } if n, err = strconv.Atoi(str[8:10]); err != nil { return } if n < 1 || n > 31 { - err = fmt.Errorf("Invalid 0000-00-000 style DATE string: " + str) + err = errors.New("Invalid 0000-00-000 style DATE string: " + str) return } d.Year = int16(y) diff --git a/pkg/utils/crypto/signers/signers.go b/pkg/utils/crypto/signers/signers.go index 60d9a7397..2692a6360 100644 --- a/pkg/utils/crypto/signers/signers.go +++ b/pkg/utils/crypto/signers/signers.go @@ -21,6 +21,7 @@ import ( "crypto/hmac" "crypto/sha1" "encoding/base64" + "errors" "fmt" "io" "net" @@ -55,7 +56,7 @@ func ValidateRequest(user config.User, req *http.Request) (bool, error) { // Verify if date headers are set, if not reject the request if req.Header.Get("x-amz-date") == "" { if req.Header.Get("Date") == "" { - return false, fmt.Errorf("Date should be set") + return false, errors.New("Date should be set") } } hm := hmac.New(sha1.New, []byte(user.SecretKey)) @@ -73,7 +74,7 @@ func ValidateRequest(user config.User, req *http.Request) (bool, error) { // fmt.Println("Header calculated: ", authHeader.String()) // fmt.Printf("%q : %x", ss, ss) if req.Header.Get("Authorization") != authHeader.String() { - return false, fmt.Errorf("Authorization header mismatch") + return false, errors.New("Authorization header mismatch") } return true, nil } diff --git a/pkg/utils/crypto/x509/generator.go b/pkg/utils/crypto/x509/generator.go index 5a1ba581c..5366f0bbf 100644 --- a/pkg/utils/crypto/x509/generator.go +++ b/pkg/utils/crypto/x509/generator.go @@ -17,6 +17,13 @@ package x509 import ( + "errors" + "fmt" + "math/big" + "net" + "os" + "time" + "crypto/ecdsa" "crypto/elliptic" "crypto/rand" @@ -24,11 +31,6 @@ import ( "crypto/x509" "crypto/x509/pkix" "encoding/pem" - "fmt" - "math/big" - "net" - "os" - "time" ) // Certificates - based on http://golang.org/src/crypto/tls/generate_cert.go @@ -91,11 +93,11 @@ func (tls *Certificates) GenerateCertificates(params Params) error { case "P521": priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader) default: - return fmt.Errorf("Unrecognized elliptic curve: %q", params.EcdsaCurve) + return errors.New("Unrecognized elliptic curve: %q", params.EcdsaCurve) } if err != nil { - return fmt.Errorf("failed to generate private key: %s", err) + return errors.New("failed to generate private key: %s", err) } var notBefore time.Time @@ -104,7 +106,7 @@ func (tls *Certificates) GenerateCertificates(params Params) error { } else { notBefore, err = time.Parse("Jan 2 15:04:05 2006", params.ValidFrom) if err != nil { - return fmt.Errorf("Failed to parse creation date: %s", err) + return errors.New("Failed to parse creation date: %s", err) } } notAfter := notBefore.Add(time.Duration(params.ValidFor)) @@ -112,7 +114,7 @@ func (tls *Certificates) GenerateCertificates(params Params) error { serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) if err != nil { - return fmt.Errorf("failed to generate serial number: %s", err) + return errors.New("failed to generate serial number: %s", err) } orgName := pkix.Name{ Organization: []string{"Minio"}, @@ -140,7 +142,7 @@ func (tls *Certificates) GenerateCertificates(params Params) error { derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv) if err != nil { - return fmt.Errorf("Failed to create certificate: %s", err) + return errors.New("Failed to create certificate: %s", err) } tls.CertPemBlock = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})