diff --git a/internal/engine/netx/dialer/dialer.go b/internal/engine/netx/dialer/dialer.go index 6f015c6..d3ab02d 100644 --- a/internal/engine/netx/dialer/dialer.go +++ b/internal/engine/netx/dialer/dialer.go @@ -6,6 +6,7 @@ import ( "net/url" "github.com/ooni/probe-cli/v3/internal/engine/netx/trace" + "github.com/ooni/probe-cli/v3/internal/errorsx" "github.com/ooni/probe-cli/v3/internal/netxlite" ) @@ -69,7 +70,7 @@ type Config struct { // New creates a new Dialer from the specified config and resolver. func New(config *Config, resolver Resolver) Dialer { var d Dialer = netxlite.DefaultDialer - d = &errorWrapperDialer{Dialer: d} + d = &errorsx.ErrorWrapperDialer{Dialer: d} if config.Logger != nil { d = &netxlite.DialerLogger{Dialer: d, Logger: config.Logger} } diff --git a/internal/engine/netx/dialer/dialer_test.go b/internal/engine/netx/dialer/dialer_test.go index 57202d2..e038bc9 100644 --- a/internal/engine/netx/dialer/dialer_test.go +++ b/internal/engine/netx/dialer/dialer_test.go @@ -7,6 +7,7 @@ import ( "github.com/apex/log" "github.com/ooni/probe-cli/v3/internal/engine/netx/trace" + "github.com/ooni/probe-cli/v3/internal/errorsx" "github.com/ooni/probe-cli/v3/internal/netxlite" ) @@ -47,7 +48,7 @@ func TestNewCreatesTheExpectedChain(t *testing.T) { if !ok { t.Fatal("not a loggingDialer") } - ewd, ok := ld.Dialer.(*errorWrapperDialer) + ewd, ok := ld.Dialer.(*errorsx.ErrorWrapperDialer) if !ok { t.Fatal("not an errorWrappingDialer") } diff --git a/internal/engine/netx/dialer/errorwrapper.go b/internal/errorsx/dialer.go similarity index 59% rename from internal/engine/netx/dialer/errorwrapper.go rename to internal/errorsx/dialer.go index ed39f6a..d92e1a3 100644 --- a/internal/engine/netx/dialer/errorwrapper.go +++ b/internal/errorsx/dialer.go @@ -1,23 +1,27 @@ -package dialer +package errorsx import ( "context" "net" - - "github.com/ooni/probe-cli/v3/internal/errorsx" ) -// errorWrapperDialer is a dialer that performs err wrapping -type errorWrapperDialer struct { +// Dialer establishes network connections. +type Dialer interface { + // DialContext behaves like net.Dialer.DialContext. + DialContext(ctx context.Context, network, address string) (net.Conn, error) +} + +// ErrorWrapperDialer is a dialer that performs err wrapping. +type ErrorWrapperDialer struct { Dialer } // DialContext implements Dialer.DialContext -func (d *errorWrapperDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { +func (d *ErrorWrapperDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { conn, err := d.Dialer.DialContext(ctx, network, address) - err = errorsx.SafeErrWrapperBuilder{ + err = SafeErrWrapperBuilder{ Error: err, - Operation: errorsx.ConnectOperation, + Operation: ConnectOperation, }.MaybeBuild() if err != nil { return nil, err @@ -33,9 +37,9 @@ type errorWrapperConn struct { // Read implements net.Conn.Read func (c *errorWrapperConn) Read(b []byte) (n int, err error) { n, err = c.Conn.Read(b) - err = errorsx.SafeErrWrapperBuilder{ + err = SafeErrWrapperBuilder{ Error: err, - Operation: errorsx.ReadOperation, + Operation: ReadOperation, }.MaybeBuild() return } @@ -43,9 +47,9 @@ func (c *errorWrapperConn) Read(b []byte) (n int, err error) { // Write implements net.Conn.Write func (c *errorWrapperConn) Write(b []byte) (n int, err error) { n, err = c.Conn.Write(b) - err = errorsx.SafeErrWrapperBuilder{ + err = SafeErrWrapperBuilder{ Error: err, - Operation: errorsx.WriteOperation, + Operation: WriteOperation, }.MaybeBuild() return } @@ -53,9 +57,9 @@ func (c *errorWrapperConn) Write(b []byte) (n int, err error) { // Close implements net.Conn.Close func (c *errorWrapperConn) Close() (err error) { err = c.Conn.Close() - err = errorsx.SafeErrWrapperBuilder{ + err = SafeErrWrapperBuilder{ Error: err, - Operation: errorsx.CloseOperation, + Operation: CloseOperation, }.MaybeBuild() return } diff --git a/internal/engine/netx/dialer/errorwrapper_test.go b/internal/errorsx/dialer_test.go similarity index 77% rename from internal/engine/netx/dialer/errorwrapper_test.go rename to internal/errorsx/dialer_test.go index bda7223..bbd2920 100644 --- a/internal/engine/netx/dialer/errorwrapper_test.go +++ b/internal/errorsx/dialer_test.go @@ -1,4 +1,4 @@ -package dialer +package errorsx import ( "context" @@ -7,13 +7,12 @@ import ( "net" "testing" - "github.com/ooni/probe-cli/v3/internal/errorsx" "github.com/ooni/probe-cli/v3/internal/netxmocks" ) func TestErrorWrapperFailure(t *testing.T) { ctx := context.Background() - d := &errorWrapperDialer{Dialer: &netxmocks.Dialer{ + d := &ErrorWrapperDialer{Dialer: &netxmocks.Dialer{ MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { return nil, io.EOF }, @@ -22,28 +21,28 @@ func TestErrorWrapperFailure(t *testing.T) { if conn != nil { t.Fatal("expected a nil conn here") } - errorWrapperCheckErr(t, err, errorsx.ConnectOperation) + errorWrapperCheckErr(t, err, ConnectOperation) } func errorWrapperCheckErr(t *testing.T, err error, op string) { if !errors.Is(err, io.EOF) { t.Fatal("expected another error here") } - var errWrapper *errorsx.ErrWrapper + var errWrapper *ErrWrapper if !errors.As(err, &errWrapper) { t.Fatal("cannot cast to ErrWrapper") } if errWrapper.Operation != op { t.Fatal("unexpected Operation") } - if errWrapper.Failure != errorsx.FailureEOFError { + if errWrapper.Failure != FailureEOFError { t.Fatal("unexpected failure") } } func TestErrorWrapperSuccess(t *testing.T) { ctx := context.Background() - d := &errorWrapperDialer{Dialer: &netxmocks.Dialer{ + d := &ErrorWrapperDialer{Dialer: &netxmocks.Dialer{ MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) { return &netxmocks.Conn{ MockRead: func(b []byte) (int, error) { @@ -69,11 +68,11 @@ func TestErrorWrapperSuccess(t *testing.T) { t.Fatal("expected non-nil conn here") } count, err := conn.Read(nil) - errorWrapperCheckIOResult(t, count, err, errorsx.ReadOperation) + errorWrapperCheckIOResult(t, count, err, ReadOperation) count, err = conn.Write(nil) - errorWrapperCheckIOResult(t, count, err, errorsx.WriteOperation) + errorWrapperCheckIOResult(t, count, err, WriteOperation) err = conn.Close() - errorWrapperCheckErr(t, err, errorsx.CloseOperation) + errorWrapperCheckErr(t, err, CloseOperation) } func errorWrapperCheckIOResult(t *testing.T, count int, err error, op string) {