[tor-commits] [goptlib/master] Rewrite 'errors.New(fmt.Sprintf(...)) -> fmt.Errorf(...)'.

dcf at torproject.org dcf at torproject.org
Mon Sep 1 02:25:59 UTC 2014


commit 74078159cffaa10160575fbe0786523346d74c52
Author: David Fifield <david at bamsoftware.com>
Date:   Sun Aug 31 19:23:24 2014 -0700

    Rewrite 'errors.New(fmt.Sprintf(...)) -> fmt.Errorf(...)'.
    
    gofmt -l -w -r 'errors.New(fmt.Sprintf(a)) -> fmt.Errorf(a)' .
    gofmt -l -w -r 'errors.New(fmt.Sprintf(a, b)) -> fmt.Errorf(a, b)' .
    gofmt -l -w -r 'errors.New(fmt.Sprintf(a, b, c)) -> fmt.Errorf(a, b, c)' .
    gofmt -l -w -r 'errors.New(a) -> fmt.Errorf(a)' .
---
 args.go  |   15 +++++++--------
 pt.go    |   21 ++++++++++-----------
 socks.go |    7 +++----
 3 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/args.go b/args.go
index f1b5a8d..7672a35 100644
--- a/args.go
+++ b/args.go
@@ -2,7 +2,6 @@ package pt
 
 import (
 	"bytes"
-	"errors"
 	"fmt"
 	"sort"
 	"strings"
@@ -48,7 +47,7 @@ func indexUnescaped(s string, term []byte) (int, string, error) {
 		if b == '\\' {
 			i++
 			if i >= len(s) {
-				return 0, "", errors.New(fmt.Sprintf("nothing following final escape in %q", s))
+				return 0, "", fmt.Errorf("nothing following final escape in %q", s)
 			}
 			b = s[i]
 		}
@@ -82,7 +81,7 @@ func parseClientParameters(s string) (args Args, err error) {
 		i += offset
 		// End of string or no equals sign?
 		if i >= len(s) || s[i] != '=' {
-			err = errors.New(fmt.Sprintf("no equals sign in %q", s[begin:i]))
+			err = fmt.Errorf("no equals sign in %q", s[begin:i])
 			return
 		}
 		// Skip the equals sign.
@@ -94,7 +93,7 @@ func parseClientParameters(s string) (args Args, err error) {
 		}
 		i += offset
 		if len(key) == 0 {
-			err = errors.New(fmt.Sprintf("empty key in %q", s[begin:i]))
+			err = fmt.Errorf("empty key in %q", s[begin:i])
 			return
 		}
 		args.Add(key, value)
@@ -132,7 +131,7 @@ func parseServerTransportOptions(s string) (opts map[string]Args, err error) {
 		i += offset
 		// End of string or no colon?
 		if i >= len(s) || s[i] != ':' {
-			err = errors.New(fmt.Sprintf("no colon in %q", s[begin:i]))
+			err = fmt.Errorf("no colon in %q", s[begin:i])
 			return
 		}
 		// Skip the colon.
@@ -145,7 +144,7 @@ func parseServerTransportOptions(s string) (opts map[string]Args, err error) {
 		i += offset
 		// End of string or no equals sign?
 		if i >= len(s) || s[i] != '=' {
-			err = errors.New(fmt.Sprintf("no equals sign in %q", s[begin:i]))
+			err = fmt.Errorf("no equals sign in %q", s[begin:i])
 			return
 		}
 		// Skip the equals sign.
@@ -157,11 +156,11 @@ func parseServerTransportOptions(s string) (opts map[string]Args, err error) {
 		}
 		i += offset
 		if len(methodName) == 0 {
-			err = errors.New(fmt.Sprintf("empty method name in %q", s[begin:i]))
+			err = fmt.Errorf("empty method name in %q", s[begin:i])
 			return
 		}
 		if len(key) == 0 {
-			err = errors.New(fmt.Sprintf("empty key in %q", s[begin:i]))
+			err = fmt.Errorf("empty key in %q", s[begin:i])
 			return
 		}
 		if opts[methodName] == nil {
diff --git a/pt.go b/pt.go
index 1ba5f0c..62dfc38 100644
--- a/pt.go
+++ b/pt.go
@@ -132,7 +132,6 @@ import (
 	"crypto/sha256"
 	"crypto/subtle"
 	"encoding/binary"
-	"errors"
 	"fmt"
 	"io"
 	"net"
@@ -513,14 +512,14 @@ func readAuthCookie(f io.Reader) ([]byte, error) {
 	// Check that the file ends here.
 	n, err = f.Read(make([]byte, 1))
 	if n != 0 {
-		return nil, errors.New(fmt.Sprintf("file is longer than 64 bytes"))
+		return nil, fmt.Errorf("file is longer than 64 bytes")
 	} else if err != io.EOF {
-		return nil, errors.New(fmt.Sprintf("did not find EOF at end of file"))
+		return nil, fmt.Errorf("did not find EOF at end of file")
 	}
 	header := buf[0:32]
 	cookie := buf[32:64]
 	if subtle.ConstantTimeCompare(header, authCookieHeader) != 1 {
-		return nil, errors.New(fmt.Sprintf("missing auth cookie header"))
+		return nil, fmt.Errorf("missing auth cookie header")
 	}
 
 	return cookie, nil
@@ -644,12 +643,12 @@ func extOrPortAuthenticate(s io.ReadWriter, info *ServerInfo) error {
 		authTypes[b] = true
 	}
 	if count >= 256 {
-		return errors.New(fmt.Sprintf("read 256 auth types without seeing \\x00"))
+		return fmt.Errorf("read 256 auth types without seeing \\x00")
 	}
 
 	// We support only type 1, SAFE_COOKIE.
 	if !authTypes[1] {
-		return errors.New(fmt.Sprintf("server didn't offer auth type 1"))
+		return fmt.Errorf("server didn't offer auth type 1")
 	}
 	_, err := s.Write([]byte{1})
 	if err != nil {
@@ -681,7 +680,7 @@ func extOrPortAuthenticate(s io.ReadWriter, info *ServerInfo) error {
 
 	expectedServerHash := computeServerHash(info.AuthCookie, clientNonce, serverNonce)
 	if subtle.ConstantTimeCompare(serverHash, expectedServerHash) != 1 {
-		return errors.New(fmt.Sprintf("mismatch in server hash"))
+		return fmt.Errorf("mismatch in server hash")
 	}
 
 	clientHash = computeClientHash(info.AuthCookie, clientNonce, serverNonce)
@@ -696,7 +695,7 @@ func extOrPortAuthenticate(s io.ReadWriter, info *ServerInfo) error {
 		return err
 	}
 	if status[0] != 1 {
-		return errors.New(fmt.Sprintf("server rejected authentication"))
+		return fmt.Errorf("server rejected authentication")
 	}
 
 	return nil
@@ -714,7 +713,7 @@ const (
 func extOrPortSendCommand(s io.Writer, cmd uint16, body []byte) error {
 	var buf bytes.Buffer
 	if len(body) > 65535 {
-		return errors.New(fmt.Sprintf("body length %d exceeds maximum of 65535", len(body)))
+		return fmt.Errorf("body length %d exceeds maximum of 65535", len(body))
 	}
 	err := binary.Write(&buf, binary.BigEndian, cmd)
 	if err != nil {
@@ -807,9 +806,9 @@ func extOrPortSetup(s io.ReadWriter, addr, methodName string) error {
 		return err
 	}
 	if cmd == extOrCmdDeny {
-		return errors.New("server returned DENY after our USERADDR and DONE")
+		return fmt.Errorf("server returned DENY after our USERADDR and DONE")
 	} else if cmd != extOrCmdOkay {
-		return errors.New(fmt.Sprintf("server returned unknown command 0x%04x after our USERADDR and DONE", cmd))
+		return fmt.Errorf("server returned unknown command 0x%04x after our USERADDR and DONE", cmd)
 	}
 
 	return nil
diff --git a/socks.go b/socks.go
index f34f78f..6ad6542 100644
--- a/socks.go
+++ b/socks.go
@@ -2,7 +2,6 @@ package pt
 
 import (
 	"bufio"
-	"errors"
 	"fmt"
 	"io"
 	"net"
@@ -166,11 +165,11 @@ func readSocks4aConnect(s io.Reader) (req SocksRequest, err error) {
 		return
 	}
 	if h[0] != socksVersion {
-		err = errors.New(fmt.Sprintf("SOCKS header had version 0x%02x, not 0x%02x", h[0], socksVersion))
+		err = fmt.Errorf("SOCKS header had version 0x%02x, not 0x%02x", h[0], socksVersion)
 		return
 	}
 	if h[1] != socksCmdConnect {
-		err = errors.New(fmt.Sprintf("SOCKS header had command 0x%02x, not 0x%02x", h[1], socksCmdConnect))
+		err = fmt.Errorf("SOCKS header had command 0x%02x, not 0x%02x", h[1], socksCmdConnect)
 		return
 	}
 
@@ -202,7 +201,7 @@ func readSocks4aConnect(s io.Reader) (req SocksRequest, err error) {
 	}
 
 	if r.Buffered() != 0 {
-		err = errors.New(fmt.Sprintf("%d bytes left after SOCKS header", r.Buffered()))
+		err = fmt.Errorf("%d bytes left after SOCKS header", r.Buffered())
 		return
 	}
 



More information about the tor-commits mailing list