Skip to content

Commit

Permalink
replace pkg/errors to eris
Browse files Browse the repository at this point in the history
Since pkg/errors is archived, transition to eris, which is a drop-in
replacement which also supports modern Go's native wrapped errors.

Signed-off-by: Seán C McCord <ulexus@gmail.com>
  • Loading branch information
Ulexus committed Nov 14, 2020
1 parent 30dca11 commit c75574a
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 75 deletions.
4 changes: 2 additions & 2 deletions _examples/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"sync"

"github.com/inconshreveable/log15"
"github.com/rotisserie/eris"

"github.com/CyCoreSystems/ari-proxy/v5/client"
"github.com/CyCoreSystems/ari/v5"
"github.com/CyCoreSystems/ari/v5/ext/play"
"github.com/CyCoreSystems/ari/v5/rid"
"github.com/pkg/errors"
)

var ariApp = "test"
Expand Down Expand Up @@ -82,7 +82,7 @@ func ensureBridge(ctx context.Context, cl ari.Client, src *ari.Key) (err error)
bridge, err = cl.Bridge().Create(key, "mixing", key.ID)
if err != nil {
bridge = nil
return errors.Wrap(err, "failed to create bridge")
return eris.Wrap(err, "failed to create bridge")
}

wg := new(sync.WaitGroup)
Expand Down
30 changes: 15 additions & 15 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"github.com/CyCoreSystems/ari-proxy/v5/proxy"
"github.com/CyCoreSystems/ari/v5"
"github.com/CyCoreSystems/ari/v5/rid"
"github.com/rotisserie/eris"

"github.com/inconshreveable/log15"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
)

// ClosureGracePeriod is the amount of time to wait after the closure of the
Expand All @@ -39,7 +39,7 @@ const DefaultInputBufferLength = 100
var DefaultClusterMaxAge = 5 * time.Minute

// ErrNil indicates that the request returned an empty response
var ErrNil = errors.New("Nil")
var ErrNil = eris.New("Nil")

// core is the core, functional piece of a Client which is the same across the
// family of derived clients. It manages stateful elements such as the bus,
Expand Down Expand Up @@ -146,14 +146,14 @@ func (c *core) Start() error {
n, err := nats.Connect(c.uri)
if err != nil {
c.close()
return errors.Wrap(err, "failed to connect to NATS")
return eris.Wrap(err, "failed to connect to NATS")
}

c.nc, err = nats.NewEncodedConn(n, nats.JSON_ENCODER)
if err != nil {
n.Close() // need this here because nc is not yet bound to the core
c.close()
return errors.Wrap(err, "failed to encode NATS connection")
return eris.Wrap(err, "failed to encode NATS connection")
}

c.closeNATSOnClose = true
Expand All @@ -166,7 +166,7 @@ func (c *core) Start() error {
err := c.maintainCluster()
if err != nil {
c.close()
return errors.Wrap(err, "failed to start cluster maintenance")
return eris.Wrap(err, "failed to start cluster maintenance")
}

return nil
Expand All @@ -177,7 +177,7 @@ func (c *core) maintainCluster() (err error) {
c.cluster.Update(o.Node, o.Application)
})
if err != nil {
return errors.Wrap(err, "failed to listen to proxy announcements")
return eris.Wrap(err, "failed to listen to proxy announcements")
}

// Send an initial ping for proxy announcements
Expand Down Expand Up @@ -230,7 +230,7 @@ func New(ctx context.Context, opts ...OptionFunc) (*Client, error) {
// Start the core, if it is not already started
err := c.core.Start()
if err != nil {
return nil, errors.Wrap(err, "failed to start core")
return nil, eris.Wrap(err, "failed to start core")
}

// Create the bus
Expand Down Expand Up @@ -529,7 +529,7 @@ func (c *Client) makeRequest(class string, req *proxy.Request) (*proxy.Response,

func (c *Client) makeRequests(class string, req *proxy.Request) (responses []*proxy.Response, err error) {
if req == nil {
return nil, errors.New("empty request")
return nil, eris.New("empty request")
}
if req.Key == nil {
req.Key = ari.NewKey("", "")
Expand All @@ -549,14 +549,14 @@ func (c *Client) makeRequests(class string, req *proxy.Request) (responses []*pr
}
})
if err != nil {
return nil, errors.Wrap(err, "failed to subscribe to data responses")
return nil, eris.Wrap(err, "failed to subscribe to data responses")
}
defer replySub.Unsubscribe() // nolint: errcheck

// Make an all-call for the entity data
err = c.core.nc.PublishRequest(c.subject(class, req), reply, req)
if err != nil {
return nil, errors.Wrap(err, "failed to make request for data")
return nil, eris.Wrap(err, "failed to make request for data")
}

// Wait for replies
Expand Down Expand Up @@ -607,7 +607,7 @@ func (f *limitedResponseForwarder) Forward(o *proxy.Response) {
// TODO: simplify
func (c *Client) makeBroadcastRequestReturnFirstGoodResponse(class string, req *proxy.Request) (*proxy.Response, error) {
if req == nil {
return nil, errors.New("empty request")
return nil, eris.New("empty request")
}

if req.Key == nil {
Expand All @@ -623,13 +623,13 @@ func (c *Client) makeBroadcastRequestReturnFirstGoodResponse(class string, req *

replySub, err := c.core.nc.Subscribe(reply, rf.Forward)
if err != nil {
return nil, errors.Wrap(err, "failed to subscribe to data responses")
return nil, eris.Wrap(err, "failed to subscribe to data responses")
}
defer replySub.Unsubscribe() // nolint: errcheck

// Make an all-call for the entity data
if err = c.core.nc.PublishRequest(c.subject(class, req), reply, req); err != nil {
return nil, errors.Wrap(err, "failed to make request for data")
return nil, eris.Wrap(err, "failed to make request for data")
}

// Wait for replies
Expand All @@ -638,14 +638,14 @@ func (c *Client) makeBroadcastRequestReturnFirstGoodResponse(class string, req *
case <-time.After(c.requestTimeout):
// Return the last error if we got one; otherwise, return a timeout error
if err == nil {
err = errors.New("timeout")
err = eris.New("timeout")
}

return nil, err
case resp, more := <-rf.fwdChan:
if !more {
if err == nil {
err = errors.New("no data")
err = eris.New("no data")
}

return nil, err
Expand Down
6 changes: 3 additions & 3 deletions client/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/CyCoreSystems/ari/v5"
"github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
)

// ListenQueue is the queue group to use for distributing StasisStart events to Listeners.
Expand All @@ -22,15 +22,15 @@ var ListenQueue = "ARIProxyStasisStartDistributorQueue"
func Listen(ctx context.Context, ac ari.Client, h func(*ari.ChannelHandle, *ari.StasisStart)) error {
c, ok := ac.(*Client)
if !ok {
return errors.New("ARI Client must be a proxy client")
return eris.New("ARI Client must be a proxy client")
}

subj := fmt.Sprintf("%sevent.%s.>", c.core.prefix, c.ApplicationName())

c.log.Debug("listening for events", "subject", subj)
sub, err := c.nc.QueueSubscribe(subj, ListenQueue, listenProcessor(ac, h))
if err != nil {
return errors.Wrap(err, "failed to subscribe to events")
return eris.Wrap(err, "failed to subscribe to events")
}
defer sub.Unsubscribe() // nolint: errcheck

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/CyCoreSystems/ari/v5 v5.0.8
github.com/CyCoreSystems/ari/v5 v5.1.2
github.com/fsnotify/fsnotify v0.0.0-20170329110642-4da3e2cfbabc // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/hashicorp/hcl v0.0.0-20170509225359-392dba7d905e // indirect
Expand All @@ -16,7 +16,7 @@ require (
github.com/nats-io/nats-server/v2 v2.0.0 // indirect
github.com/nats-io/nats.go v1.8.1
github.com/pelletier/go-toml v0.0.0-20170817000623-4692b8f9babf // indirect
github.com/pkg/errors v0.8.1
github.com/rotisserie/eris v0.4.1
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab // indirect
github.com/spf13/cast v1.1.0 // indirect
github.com/spf13/cobra v0.0.2
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/CyCoreSystems/ari/v5 v5.0.8 h1:tyH0P1cSTR4T7fKzuq+3mUb7We9ksosIOp5yhO1HQ5A=
github.com/CyCoreSystems/ari/v5 v5.0.8/go.mod h1:X/wSaIuDdleDdI9gK3s6EuPFuBDFKYoUUzOPqVeOqPA=
github.com/CyCoreSystems/ari/v5 v5.1.1 h1:BGPqCIQzgKDpF89L9RWixbXDnr9QZbc93Z2gDusajNk=
github.com/CyCoreSystems/ari/v5 v5.1.1/go.mod h1:oSoeAuafmlIRrsIlQEQXpyo2k2sCBBVBVtaNXyTixSw=
github.com/CyCoreSystems/ari/v5 v5.1.2 h1:/Ze75yyANGLo1VhhJZ69dXfdeVhc9pvi+AnQWTPkTws=
github.com/CyCoreSystems/ari/v5 v5.1.2/go.mod h1:oSoeAuafmlIRrsIlQEQXpyo2k2sCBBVBVtaNXyTixSw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -53,10 +55,10 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v0.0.0-20170817000623-4692b8f9babf h1:XjB5kVAWxe9qH3eZIUfsW4gLh7prYlRJmZ5zRjyvCeg=
github.com/pelletier/go-toml v0.0.0-20170817000623-4692b8f9babf/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rotisserie/eris v0.4.1 h1:0IHaklBg2X5z10qpXS8F6eR3bUM2Xsbr1bH8W/eLUlo=
github.com/rotisserie/eris v0.4.1/go.mod h1:lODN/gtqebxPHRbCcWeCYOE350FC2M3V/oAPT2wKxAU=
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab h1:IVAbBHQR8rXL2Fc8Zba/lMF7KOnTi70lqdx91UTuAwQ=
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.1.0 h1:0Rhw4d6C8J9VPu6cjZLIhZ8+aAOHcDvGeKn+cq5Aq3k=
Expand Down
14 changes: 7 additions & 7 deletions internal/integration/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/CyCoreSystems/ari/v5"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
tmock "github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -60,12 +60,12 @@ func TestApplicationData(t *testing.T, s Server) {
})

runTest("error", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
expected := eris.New("unknown error")

m.Application.On("Data", key).Return(nil, expected)

res, err := cl.Application().Data(key)
if err == nil || errors.Cause(err).Error() != expected.Error() {
if err == nil || eris.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}
if res != nil {
Expand Down Expand Up @@ -94,11 +94,11 @@ func TestApplicationSubscribe(t *testing.T, s Server) {
})

runTest("error", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
expected := eris.New("unknown error")

m.Application.On("Subscribe", key, "2").Return(expected)

if err := cl.Application().Subscribe(key, "2"); err == nil || errors.Cause(err).Error() != expected.Error() {
if err := cl.Application().Subscribe(key, "2"); err == nil || eris.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}

Expand All @@ -124,11 +124,11 @@ func TestApplicationUnsubscribe(t *testing.T, s Server) {
})

runTest("error", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
expected := eris.New("unknown error")

m.Application.On("Unsubscribe", key, "2").Return(expected)

if err := cl.Application().Unsubscribe(key, "2"); err == nil || errors.Cause(err).Error() != expected.Error() {
if err := cl.Application().Unsubscribe(key, "2"); err == nil || eris.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}

Expand Down
12 changes: 6 additions & 6 deletions internal/integration/asterisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/CyCoreSystems/ari/v5"
"github.com/CyCoreSystems/ari/v5/client/arimocks"
"github.com/pkg/errors"
"github.com/rotisserie/eris"
)

func TestAsteriskInfo(t *testing.T, s Server) {
Expand All @@ -29,12 +29,12 @@ func TestAsteriskInfo(t *testing.T, s Server) {
})

runTest("noFilterError", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
expected := eris.New("unknown error")

m.Asterisk.On("Info", ari.NodeKey("asdf", "1")).Return(nil, expected)

ret, err := cl.Asterisk().Info(ari.NodeKey("asdf", "1"))
if err == nil || errors.Cause(err).Error() != expected.Error() {
if err == nil || eris.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}
if ret != nil {
Expand Down Expand Up @@ -73,15 +73,15 @@ func TestAsteriskVariablesGet(t *testing.T, s Server) {
})

runTest("error", t, s, func(t *testing.T, m *mock, cl ari.Client) {
expected := errors.New("unknown error")
expected := eris.New("unknown error")

mv := arimocks.AsteriskVariables{}
mv.On("Get", key).Return("", expected)

m.Asterisk.On("Variables").Return(&mv)

ret, err := cl.Asterisk().Variables().Get(key)
if err == nil || errors.Cause(err).Error() != expected.Error() {
if err == nil || eris.Cause(err).Error() != expected.Error() {
t.Errorf("Expected error '%v', got '%v'", expected, err)
}
if ret != "" {
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestAsteriskVariablesSet(t *testing.T, s Server) {

mv := arimocks.AsteriskVariables{}
m.Asterisk.On("Variables").Return(&mv)
mv.On("Set", key, "hello").Return(errors.New("error"))
mv.On("Set", key, "hello").Return(eris.New("error"))

err := cl.Asterisk().Variables().Set(key, "hello")
if err == nil {
Expand Down
Loading

0 comments on commit c75574a

Please sign in to comment.