all: Tweak error creation (#6391)

- In the few places where we wrap errors, use the new Go 1.13 "%w"
  construction instead of %s or %v.

- Where we create errors with constant strings, consistently use
  errors.New and not fmt.Errorf.

- Remove capitalization from errors in the few places where we had that.
This commit is contained in:
Jakob Borg
2020-03-03 22:40:00 +01:00
committed by GitHub
parent eddc8d3ff2
commit dd92b2b8f4
30 changed files with 67 additions and 60 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ type RelayClient interface {
func NewClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) (RelayClient, error) {
factory, ok := supportedSchemes[uri.Scheme]
if !ok {
return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
}
return factory(uri, certs, invitations, timeout), nil
+2 -1
View File
@@ -6,6 +6,7 @@ import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
@@ -100,7 +101,7 @@ func (c *dynamicClient) serve(ctx context.Context) error {
}
}
l.Debugln(c, "could not find a connectable relay")
return fmt.Errorf("could not find a connectable relay")
return errors.New("could not find a connectable relay")
}
func (c *dynamicClient) Stop() {
+3 -3
View File
@@ -19,7 +19,7 @@ import (
func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingprotocol.DeviceID, certs []tls.Certificate, timeout time.Duration) (protocol.SessionInvitation, error) {
if uri.Scheme != "relay" {
return protocol.SessionInvitation{}, fmt.Errorf("Unsupported relay scheme: %v", uri.Scheme)
return protocol.SessionInvitation{}, fmt.Errorf("unsupported relay scheme: %v", uri.Scheme)
}
ctx, cancel := context.WithTimeout(ctx, timeout)
@@ -53,7 +53,7 @@ func GetInvitationFromRelay(ctx context.Context, uri *url.URL, id syncthingproto
switch msg := message.(type) {
case protocol.Response:
return protocol.SessionInvitation{}, fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
return protocol.SessionInvitation{}, fmt.Errorf("incorrect response code %d: %s", msg.Code, msg.Message)
case protocol.SessionInvitation:
l.Debugln("Received invitation", msg, "via", conn.LocalAddr())
ip := net.IP(msg.Address)
@@ -96,7 +96,7 @@ func JoinSession(ctx context.Context, invitation protocol.SessionInvitation) (ne
switch msg := message.(type) {
case protocol.Response:
if msg.Code != 0 {
return nil, fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
return nil, fmt.Errorf("incorrect response code %d: %s", msg.Code, msg.Message)
}
return conn, nil
default:
+7 -7
View File
@@ -73,9 +73,9 @@ func (c *staticClient) serve(ctx context.Context) error {
c.mut.Unlock()
messages := make(chan interface{})
errors := make(chan error, 1)
errorsc := make(chan error, 1)
go messageReader(ctx, c.conn, messages, errors)
go messageReader(ctx, c.conn, messages, errorsc)
timeout := time.NewTimer(c.messageTimeout)
@@ -102,7 +102,7 @@ func (c *staticClient) serve(ctx context.Context) error {
case protocol.RelayFull:
l.Infof("Disconnected from relay %s due to it becoming full.", c.uri)
return fmt.Errorf("relay full")
return errors.New("relay full")
default:
l.Infoln("Relay: protocol error: unexpected message %v", msg)
@@ -113,13 +113,13 @@ func (c *staticClient) serve(ctx context.Context) error {
l.Debugln(c, "stopping")
return nil
case err := <-errors:
case err := <-errorsc:
l.Infof("Disconnecting from relay %s due to error: %s", c.uri, err)
return err
case <-timeout.C:
l.Debugln(c, "timed out")
return fmt.Errorf("timed out")
return errors.New("timed out")
}
}
}
@@ -205,7 +205,7 @@ func (c *staticClient) join() error {
}
case protocol.RelayFull:
return fmt.Errorf("relay full")
return errors.New("relay full")
default:
return fmt.Errorf("protocol error: expecting response got %v", msg)
@@ -221,7 +221,7 @@ func performHandshakeAndValidation(conn *tls.Conn, uri *url.URL) error {
cs := conn.ConnectionState()
if !cs.NegotiatedProtocolIsMutual || cs.NegotiatedProtocol != protocol.ProtocolName {
return fmt.Errorf("protocol negotiation error")
return errors.New("protocol negotiation error")
}
q := uri.Query()
+4 -4
View File
@@ -3,7 +3,7 @@
package protocol
import (
"fmt"
"errors"
"io"
)
@@ -53,7 +53,7 @@ func WriteMessage(w io.Writer, message interface{}) error {
payload, err = msg.MarshalXDR()
header.messageType = messageTypeRelayFull
default:
err = fmt.Errorf("Unknown message type")
err = errors.New("unknown message type")
}
if err != nil {
@@ -84,7 +84,7 @@ func ReadMessage(r io.Reader) (interface{}, error) {
}
if header.magic != magic {
return nil, fmt.Errorf("magic mismatch")
return nil, errors.New("magic mismatch")
}
buf = make([]byte, int(header.messageLength))
@@ -127,5 +127,5 @@ func ReadMessage(r io.Reader) (interface{}, error) {
return msg, err
}
return nil, fmt.Errorf("Unknown message type")
return nil, errors.New("unknown message type")
}