Skip to content

Commit

Permalink
all: drop x/xerrors in favor of fmt+errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sbinet committed Oct 21, 2020
1 parent 361b05f commit 2e2a862
Show file tree
Hide file tree
Showing 30 changed files with 287 additions and 299 deletions.
46 changes: 23 additions & 23 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package zmq4
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"

"golang.org/x/xerrors"
)

var ErrClosedConn = xerrors.New("zmq4: read/write on closed connection")
var ErrClosedConn = errors.New("zmq4: read/write on closed connection")

// Conn implements the ZeroMQ Message Transport Protocol as defined
// in https://rfc.zeromq.org/spec:23/ZMTP/.
Expand Down Expand Up @@ -66,11 +66,11 @@ func (c *Conn) Write(p []byte) (int, error) {
// Open performs a complete ZMTP handshake.
func Open(rw net.Conn, sec Security, sockType SocketType, sockID SocketIdentity, server bool, onCloseErrorCB func(c *Conn)) (*Conn, error) {
if rw == nil {
return nil, xerrors.Errorf("zmq4: invalid nil read-writer")
return nil, fmt.Errorf("zmq4: invalid nil read-writer")
}

if sec == nil {
return nil, xerrors.Errorf("zmq4: invalid nil security")
return nil, fmt.Errorf("zmq4: invalid nil security")
}

conn := &Conn{
Expand All @@ -89,7 +89,7 @@ func Open(rw net.Conn, sec Security, sockType SocketType, sockID SocketIdentity,

err := conn.init(sec)
if err != nil {
return nil, xerrors.Errorf("zmq4: could not initialize ZMTP connection: %w", err)
return nil, fmt.Errorf("zmq4: could not initialize ZMTP connection: %w", err)
}

return conn, nil
Expand All @@ -101,17 +101,17 @@ func (conn *Conn) init(sec Security) error {

err = conn.greet(conn.Server)
if err != nil {
return xerrors.Errorf("zmq4: could not exchange greetings: %w", err)
return fmt.Errorf("zmq4: could not exchange greetings: %w", err)
}

err = conn.sec.Handshake(conn, conn.Server)
if err != nil {
return xerrors.Errorf("zmq4: could not perform security handshake: %w", err)
return fmt.Errorf("zmq4: could not perform security handshake: %w", err)
}

peer := SocketType(conn.Peer.Meta[sysSockType])
if !peer.IsCompatible(conn.typ) {
return xerrors.Errorf("zmq4: peer=%q not compatible with %q", peer, conn.typ)
return fmt.Errorf("zmq4: peer=%q not compatible with %q", peer, conn.typ)
}

// FIXME(sbinet): if security mechanism does not define a client/server
Expand All @@ -136,14 +136,14 @@ func (conn *Conn) greet(server bool) error {
err = send.write(conn.rw)
if err != nil {
conn.checkIO(err)
return xerrors.Errorf("zmq4: could not send greeting: %w", err)
return fmt.Errorf("zmq4: could not send greeting: %w", err)
}

var recv greeting
err = recv.read(conn.rw)
if err != nil {
conn.checkIO(err)
return xerrors.Errorf("zmq4: could not recv greeting: %w", err)
return fmt.Errorf("zmq4: could not recv greeting: %w", err)
}

peerKind := asString(recv.Mechanism[:])
Expand All @@ -153,7 +153,7 @@ func (conn *Conn) greet(server bool) error {

conn.Peer.Server, err = asBool(recv.Server)
if err != nil {
return xerrors.Errorf("zmq4: could not get peer server flag: %w", err)
return fmt.Errorf("zmq4: could not get peer server flag: %w", err)
}

return nil
Expand Down Expand Up @@ -189,7 +189,7 @@ func (c *Conn) SendMsg(msg Msg) error {
}
err := c.send(false, frame, flag)
if err != nil {
return xerrors.Errorf("zmq4: error sending frame %d/%d: %w", i+1, nframes, err)
return fmt.Errorf("zmq4: error sending frame %d/%d: %w", i+1, nframes, err)
}
}
return nil
Expand All @@ -202,7 +202,7 @@ func (c *Conn) RecvMsg() (Msg, error) {
}
msg := c.read()
if msg.err != nil {
return msg, xerrors.Errorf("zmq4: could not read recv msg: %w", msg.err)
return msg, fmt.Errorf("zmq4: could not read recv msg: %w", msg.err)
}

if !msg.isCmd() {
Expand All @@ -211,19 +211,19 @@ func (c *Conn) RecvMsg() (Msg, error) {

switch len(msg.Frames) {
case 0:
msg.err = xerrors.Errorf("zmq4: empty command")
msg.err = fmt.Errorf("zmq4: empty command")
return msg, msg.err
case 1:
// ok
default:
msg.err = xerrors.Errorf("zmq4: invalid length command")
msg.err = fmt.Errorf("zmq4: invalid length command")
return msg, msg.err
}

var cmd Cmd
msg.err = cmd.unmarshalZMTP(msg.Frames[0])
if msg.err != nil {
return msg, xerrors.Errorf("zmq4: could not unmarshal ZMTP recv msg: %w", msg.err)
return msg, fmt.Errorf("zmq4: could not unmarshal ZMTP recv msg: %w", msg.err)
}

switch cmd.Name {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (c *Conn) RecvCmd() (Cmd, error) {

msg := c.read()
if msg.err != nil {
return cmd, xerrors.Errorf("zmq4: could not read recv cmd: %w", msg.err)
return cmd, fmt.Errorf("zmq4: could not read recv cmd: %w", msg.err)
}

if !msg.isCmd() {
Expand All @@ -263,18 +263,18 @@ func (c *Conn) RecvCmd() (Cmd, error) {

switch len(msg.Frames) {
case 0:
msg.err = xerrors.Errorf("zmq4: empty command")
msg.err = fmt.Errorf("zmq4: empty command")
return cmd, msg.err
case 1:
// ok
default:
msg.err = xerrors.Errorf("zmq4: invalid length command")
msg.err = fmt.Errorf("zmq4: invalid length command")
return cmd, msg.err
}

err := cmd.unmarshalZMTP(msg.Frames[0])
if err != nil {
return cmd, xerrors.Errorf("zmq4: could not unmarshal ZMTP recv cmd: %w", err)
return cmd, fmt.Errorf("zmq4: could not unmarshal ZMTP recv cmd: %w", err)
}

return cmd, nil
Expand Down Expand Up @@ -482,13 +482,13 @@ func (conn *Conn) checkIO(err error) {
return
}

if err == io.EOF || xerrors.Is(err, io.EOF) {
if err == io.EOF || errors.Is(err, io.EOF) {
conn.SetClosed()
return
}

var e net.Error
if xerrors.As(err, &e); e != nil && !e.Timeout() {
if errors.As(err, &e); e != nil && !e.Timeout() {
conn.SetClosed()
}
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ go 1.13
require (
github.com/go-zeromq/goczmq/v4 v4.2.2
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ github.com/go-zeromq/goczmq/v4 v4.2.2 h1:HAJN+i+3NW55ijMJJhk7oWxHKXgAuSBkoFfvr8b
github.com/go-zeromq/goczmq/v4 v4.2.2/go.mod h1:Sm/lxrfxP/Oxqs0tnHD6WAhwkWrx+S+1MRrKzcxoaYE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10 changes: 5 additions & 5 deletions internal/inproc/inproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
package inproc

import (
"errors"
"fmt"
"net"
"strings"
"sync"

"golang.org/x/xerrors"
)

var (
mgr = context{db: make(map[string]*Listener)}

ErrClosed = xerrors.New("inproc: connection closed")
ErrConnRefused = xerrors.New("inproc: connection refused")
ErrClosed = errors.New("inproc: connection closed")
ErrConnRefused = errors.New("inproc: connection refused")
)

func init() {
Expand Down Expand Up @@ -96,7 +96,7 @@ func Listen(addr string) (*Listener, error) {
_, dup := mgr.db[addr]
if dup {
mgr.mu.Unlock()
return nil, xerrors.Errorf("inproc: address %q already in use", addr)
return nil, fmt.Errorf("inproc: address %q already in use", addr)
}

l := &Listener{
Expand Down
24 changes: 12 additions & 12 deletions internal/inproc/inproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package inproc

import (
"bytes"
"fmt"
"io"
"math/rand"
"reflect"
"testing"

"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

func TestBasicIO(t *testing.T) {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRW(t *testing.T) {
grp.Go(func() error {
conn, err := lst.Accept()
if err != nil {
return xerrors.Errorf("could not accept connection: %w", err)
return fmt.Errorf("could not accept connection: %w", err)
}
defer conn.Close()

Expand All @@ -101,26 +101,26 @@ func TestRW(t *testing.T) {
raw := make([]byte, len("HELLO"))
_, err = io.ReadFull(conn, raw)
if err != nil {
return xerrors.Errorf("could not read request: %w", err)
return fmt.Errorf("could not read request: %w", err)
}

if got, want := raw, []byte("HELLO"); !reflect.DeepEqual(got, want) {
return xerrors.Errorf("invalid request: got=%v, want=%v", got, want)
return fmt.Errorf("invalid request: got=%v, want=%v", got, want)
}

_, err = conn.Write([]byte("HELLO"))
if err != nil {
return xerrors.Errorf("could not write reply: %w", err)
return fmt.Errorf("could not write reply: %w", err)
}

raw = make([]byte, len("QUIT"))
_, err = io.ReadFull(conn, raw)
if err != nil {
return xerrors.Errorf("could not read final request: %w", err)
return fmt.Errorf("could not read final request: %w", err)
}

if got, want := raw, []byte("QUIT"); !reflect.DeepEqual(got, want) {
return xerrors.Errorf("invalid request: got=%v, want=%v", got, want)
return fmt.Errorf("invalid request: got=%v, want=%v", got, want)
}

return nil
Expand All @@ -129,7 +129,7 @@ func TestRW(t *testing.T) {
grp.Go(func() error {
conn, err := Dial("inproc://rw-srv")
if err != nil {
return xerrors.Errorf("could not dial server: %w", err)
return fmt.Errorf("could not dial server: %w", err)
}
defer conn.Close()

Expand All @@ -145,22 +145,22 @@ func TestRW(t *testing.T) {

_, err = conn.Write([]byte("HELLO"))
if err != nil {
return xerrors.Errorf("could not send request: %w", err)
return fmt.Errorf("could not send request: %w", err)
}

raw := make([]byte, len("HELLO"))
_, err = io.ReadFull(conn, raw)
if err != nil {
return xerrors.Errorf("could not read reply: %w", err)
return fmt.Errorf("could not read reply: %w", err)
}

if got, want := raw, []byte("HELLO"); !reflect.DeepEqual(got, want) {
return xerrors.Errorf("invalid reply: got=%v, want=%v", got, want)
return fmt.Errorf("invalid reply: got=%v, want=%v", got, want)
}

_, err = conn.Write([]byte("QUIT"))
if err != nil {
return xerrors.Errorf("could not write final request: %w", err)
return fmt.Errorf("could not write final request: %w", err)
}

return nil
Expand Down
30 changes: 15 additions & 15 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ package zmq4
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strings"

"golang.org/x/xerrors"
)

var (
errGreeting = xerrors.New("zmq4: invalid greeting received")
errSecMech = xerrors.New("zmq4: invalid security mechanism")
errBadSec = xerrors.New("zmq4: invalid or unsupported security mechanism")
ErrBadCmd = xerrors.New("zmq4: invalid command name")
ErrBadFrame = xerrors.New("zmq4: invalid frame")
errOverflow = xerrors.New("zmq4: overflow")
errEmptyAppMDKey = xerrors.New("zmq4: empty application metadata key")
errDupAppMDKey = xerrors.New("zmq4: duplicate application metadata key")
errBoolCnv = xerrors.New("zmq4: invalid byte to bool conversion")
errGreeting = errors.New("zmq4: invalid greeting received")
errSecMech = errors.New("zmq4: invalid security mechanism")
errBadSec = errors.New("zmq4: invalid or unsupported security mechanism")
ErrBadCmd = errors.New("zmq4: invalid command name")
ErrBadFrame = errors.New("zmq4: invalid frame")
errOverflow = errors.New("zmq4: overflow")
errEmptyAppMDKey = errors.New("zmq4: empty application metadata key")
errDupAppMDKey = errors.New("zmq4: duplicate application metadata key")
errBoolCnv = errors.New("zmq4: invalid byte to bool conversion")
)

const (
Expand Down Expand Up @@ -95,21 +95,21 @@ func (g *greeting) read(r io.Reader) error {
var data [zmtpMsgLen]byte
_, err := io.ReadFull(r, data[:])
if err != nil {
return xerrors.Errorf("could not read ZMTP greeting: %w", err)
return fmt.Errorf("could not read ZMTP greeting: %w", err)
}

g.unmarshal(data[:])

if g.Sig.Header != sigHeader {
return xerrors.Errorf("invalid ZMTP signature header: %w", errGreeting)
return fmt.Errorf("invalid ZMTP signature header: %w", errGreeting)
}

if g.Sig.Footer != sigFooter {
return xerrors.Errorf("invalid ZMTP signature footer: %w", errGreeting)
return fmt.Errorf("invalid ZMTP signature footer: %w", errGreeting)
}

if !g.validate(defaultVersion) {
return xerrors.Errorf(
return fmt.Errorf(
"invalid ZMTP version (got=%v, want=%v): %w",
g.Version, defaultVersion, errGreeting,
)
Expand Down
Loading

0 comments on commit 2e2a862

Please sign in to comment.