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:40:30 -07:00
parent a737246f92
commit 596eeb097e
4 changed files with 29 additions and 24 deletions

View File

@ -17,6 +17,7 @@
package minioapi package minioapi
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -58,28 +59,28 @@ func newRange(req *http.Request, size int64) (*httpRange, error) {
// parseRange parses a Range header string as per RFC 2616. // parseRange parses a Range header string as per RFC 2616.
func (r *httpRange) parseRange(s string) error { func (r *httpRange) parseRange(s string) error {
if s == "" { if s == "" {
return fmt.Errorf("header not present") return errors.New("header not present")
} }
if !strings.HasPrefix(s, b) { if !strings.HasPrefix(s, b) {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
ras := strings.Split(s[len(b):], ",") ras := strings.Split(s[len(b):], ",")
if len(ras) == 0 { 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 // Just pick the first one and ignore the rest, we only support one range per object
if len(ras) > 1 { if len(ras) > 1 {
return fmt.Errorf("multiple ranges specified") return errors.New("multiple ranges specified")
} }
ra := strings.TrimSpace(ras[0]) ra := strings.TrimSpace(ras[0])
if ra == "" { if ra == "" {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
i := strings.Index(ra, "-") i := strings.Index(ra, "-")
if i < 0 { if i < 0 {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:]) start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
if start == "" { if start == "" {
@ -87,7 +88,7 @@ func (r *httpRange) parseRange(s string) error {
// range start relative to the end of the file. // range start relative to the end of the file.
i, err := strconv.ParseInt(end, 10, 64) i, err := strconv.ParseInt(end, 10, 64)
if err != nil { if err != nil {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
if i > r.size { if i > r.size {
i = r.size i = r.size
@ -97,7 +98,7 @@ func (r *httpRange) parseRange(s string) error {
} else { } else {
i, err := strconv.ParseInt(start, 10, 64) i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i > r.size || i < 0 { if err != nil || i > r.size || i < 0 {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
r.start = i r.start = i
if end == "" { if end == "" {
@ -106,7 +107,7 @@ func (r *httpRange) parseRange(s string) error {
} else { } else {
i, err := strconv.ParseInt(end, 10, 64) i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.start > i { if err != nil || r.start > i {
return fmt.Errorf("invalid range") return errors.New("invalid range")
} }
if i >= r.size { if i >= r.size {
i = r.size - 1 i = r.size - 1

View File

@ -1,6 +1,7 @@
package storage package storage
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -34,7 +35,7 @@ func parseDate(str string) (d Date, err error) {
y, m, n int y, m, n int
) )
if len(str) != 10 || str[4] != '-' || str[7] != '-' { 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 return
} }
if y, err = strconv.Atoi(str[0:4]); err != nil { if y, err = strconv.Atoi(str[0:4]); err != nil {
@ -44,14 +45,14 @@ func parseDate(str string) (d Date, err error) {
return return
} }
if m < 1 || m > 12 { 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 return
} }
if n, err = strconv.Atoi(str[8:10]); err != nil { if n, err = strconv.Atoi(str[8:10]); err != nil {
return return
} }
if n < 1 || n > 31 { 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 return
} }
d.Year = int16(y) d.Year = int16(y)

View File

@ -21,6 +21,7 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io" "io"
"net" "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 // Verify if date headers are set, if not reject the request
if req.Header.Get("x-amz-date") == "" { if req.Header.Get("x-amz-date") == "" {
if req.Header.Get("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)) 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.Println("Header calculated: ", authHeader.String())
// fmt.Printf("%q : %x", ss, ss) // fmt.Printf("%q : %x", ss, ss)
if req.Header.Get("Authorization") != authHeader.String() { if req.Header.Get("Authorization") != authHeader.String() {
return false, fmt.Errorf("Authorization header mismatch") return false, errors.New("Authorization header mismatch")
} }
return true, nil return true, nil
} }

View File

@ -17,6 +17,13 @@
package x509 package x509
import ( import (
"errors"
"fmt"
"math/big"
"net"
"os"
"time"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
@ -24,11 +31,6 @@ import (
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"fmt"
"math/big"
"net"
"os"
"time"
) )
// Certificates - based on http://golang.org/src/crypto/tls/generate_cert.go // 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": case "P521":
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader) priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
default: default:
return fmt.Errorf("Unrecognized elliptic curve: %q", params.EcdsaCurve) return errors.New("Unrecognized elliptic curve: %q", params.EcdsaCurve)
} }
if err != nil { 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 var notBefore time.Time
@ -104,7 +106,7 @@ func (tls *Certificates) GenerateCertificates(params Params) error {
} else { } else {
notBefore, err = time.Parse("Jan 2 15:04:05 2006", params.ValidFrom) notBefore, err = time.Parse("Jan 2 15:04:05 2006", params.ValidFrom)
if err != nil { 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)) 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) serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil { 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{ orgName := pkix.Name{
Organization: []string{"Minio"}, 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) derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
if err != nil { 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}) tls.CertPemBlock = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})