Skip to content

Commit

Permalink
add Unwrap method to allow to detect root cause error with errors.Is (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Pro authored Jan 20, 2025
1 parent a89001a commit 757a14b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: actions/checkout@v2

- name: Start Centrifugo
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --client_insecure
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --client.insecure

- name: Test
run: go test -race -v
28 changes: 26 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (t TransportError) Error() string {
return fmt.Sprintf("transport error: %v", t.Err)
}

func (t TransportError) Unwrap() error {
return t.Err
}

type ConnectError struct {
Err error
}
Expand All @@ -41,6 +45,10 @@ func (c ConnectError) Error() string {
return fmt.Sprintf("connect error: %v", c.Err)
}

func (c ConnectError) Unwrap() error {
return c.Err
}

type RefreshError struct {
Err error
}
Expand All @@ -49,12 +57,20 @@ func (r RefreshError) Error() string {
return fmt.Sprintf("refresh error: %v", r.Err)
}

func (r RefreshError) Unwrap() error {
return r.Err
}

type ConfigurationError struct {
Err error
}

func (r ConfigurationError) Error() string {
return fmt.Sprintf("configuration error: %v", r.Err)
func (c ConfigurationError) Error() string {
return fmt.Sprintf("configuration error: %v", c.Err)
}

func (c ConfigurationError) Unwrap() error {
return c.Err
}

type SubscriptionSubscribeError struct {
Expand All @@ -65,10 +81,18 @@ func (s SubscriptionSubscribeError) Error() string {
return fmt.Sprintf("subscribe error: %v", s.Err)
}

func (s SubscriptionSubscribeError) Unwrap() error {
return s.Err
}

type SubscriptionRefreshError struct {
Err error
}

func (s SubscriptionRefreshError) Error() string {
return fmt.Sprintf("refresh error: %v", s.Err)
}

func (s SubscriptionRefreshError) Unwrap() error {
return s.Err
}
73 changes: 73 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package centrifuge_test

import (
"errors"
"strings"
"testing"

"github.com/centrifugal/centrifuge-go"
)

func TestErrors(t *testing.T) {
cases := []struct {
name string
rootError error
factory func(err error) error
}{
{
name: "SubscriptionSubscribeError",
rootError: centrifuge.ErrTimeout,
factory: func(err error) error {
return centrifuge.SubscriptionSubscribeError{Err: err}
},
},
{
name: "SubscriptionRefreshError",
rootError: centrifuge.ErrUnauthorized,
factory: func(err error) error {
return centrifuge.SubscriptionRefreshError{Err: err}
},
},
{
name: "ConfigurationError",
rootError: centrifuge.ErrClientClosed,
factory: func(err error) error {
return centrifuge.ConfigurationError{Err: err}
},
},
{
name: "RefreshError",
rootError: centrifuge.ErrClientClosed,
factory: func(err error) error {
return centrifuge.RefreshError{Err: err}
},
},
{
name: "ConnectError",
rootError: centrifuge.ErrClientClosed,
factory: func(err error) error {
return centrifuge.ConnectError{Err: err}
},
},
{
name: "TransportError",
rootError: centrifuge.ErrClientClosed,
factory: func(err error) error {
return centrifuge.TransportError{Err: err}
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
err := c.factory(c.rootError)
parts := strings.Split(err.Error(), ": ")
if parts[1] != c.rootError.Error() {
t.Errorf("unexpected error string: %v", err)
}

if !errors.Is(err, c.rootError) {
t.Errorf("expected root error to be wrapped")
}
})
}
}

0 comments on commit 757a14b

Please sign in to comment.