refactor: move dialer's errorwrapper in i/errorsx (#417)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
72acd175a0
commit
ceefcaf45e
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
"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"
|
"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.
|
// New creates a new Dialer from the specified config and resolver.
|
||||||
func New(config *Config, resolver Resolver) Dialer {
|
func New(config *Config, resolver Resolver) Dialer {
|
||||||
var d Dialer = netxlite.DefaultDialer
|
var d Dialer = netxlite.DefaultDialer
|
||||||
d = &errorWrapperDialer{Dialer: d}
|
d = &errorsx.ErrorWrapperDialer{Dialer: d}
|
||||||
if config.Logger != nil {
|
if config.Logger != nil {
|
||||||
d = &netxlite.DialerLogger{Dialer: d, Logger: config.Logger}
|
d = &netxlite.DialerLogger{Dialer: d, Logger: config.Logger}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
"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"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,7 +48,7 @@ func TestNewCreatesTheExpectedChain(t *testing.T) {
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not a loggingDialer")
|
t.Fatal("not a loggingDialer")
|
||||||
}
|
}
|
||||||
ewd, ok := ld.Dialer.(*errorWrapperDialer)
|
ewd, ok := ld.Dialer.(*errorsx.ErrorWrapperDialer)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not an errorWrappingDialer")
|
t.Fatal("not an errorWrappingDialer")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,27 @@
|
||||||
package dialer
|
package errorsx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// errorWrapperDialer is a dialer that performs err wrapping
|
// Dialer establishes network connections.
|
||||||
type errorWrapperDialer struct {
|
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
|
Dialer
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements Dialer.DialContext
|
// 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)
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||||
err = errorsx.SafeErrWrapperBuilder{
|
err = SafeErrWrapperBuilder{
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorsx.ConnectOperation,
|
Operation: ConnectOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -33,9 +37,9 @@ type errorWrapperConn struct {
|
||||||
// Read implements net.Conn.Read
|
// Read implements net.Conn.Read
|
||||||
func (c *errorWrapperConn) Read(b []byte) (n int, err error) {
|
func (c *errorWrapperConn) Read(b []byte) (n int, err error) {
|
||||||
n, err = c.Conn.Read(b)
|
n, err = c.Conn.Read(b)
|
||||||
err = errorsx.SafeErrWrapperBuilder{
|
err = SafeErrWrapperBuilder{
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorsx.ReadOperation,
|
Operation: ReadOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -43,9 +47,9 @@ func (c *errorWrapperConn) Read(b []byte) (n int, err error) {
|
||||||
// Write implements net.Conn.Write
|
// Write implements net.Conn.Write
|
||||||
func (c *errorWrapperConn) Write(b []byte) (n int, err error) {
|
func (c *errorWrapperConn) Write(b []byte) (n int, err error) {
|
||||||
n, err = c.Conn.Write(b)
|
n, err = c.Conn.Write(b)
|
||||||
err = errorsx.SafeErrWrapperBuilder{
|
err = SafeErrWrapperBuilder{
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorsx.WriteOperation,
|
Operation: WriteOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -53,9 +57,9 @@ func (c *errorWrapperConn) Write(b []byte) (n int, err error) {
|
||||||
// Close implements net.Conn.Close
|
// Close implements net.Conn.Close
|
||||||
func (c *errorWrapperConn) Close() (err error) {
|
func (c *errorWrapperConn) Close() (err error) {
|
||||||
err = c.Conn.Close()
|
err = c.Conn.Close()
|
||||||
err = errorsx.SafeErrWrapperBuilder{
|
err = SafeErrWrapperBuilder{
|
||||||
Error: err,
|
Error: err,
|
||||||
Operation: errorsx.CloseOperation,
|
Operation: CloseOperation,
|
||||||
}.MaybeBuild()
|
}.MaybeBuild()
|
||||||
return
|
return
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package dialer
|
package errorsx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -7,13 +7,12 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestErrorWrapperFailure(t *testing.T) {
|
func TestErrorWrapperFailure(t *testing.T) {
|
||||||
ctx := context.Background()
|
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) {
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
},
|
},
|
||||||
|
@ -22,28 +21,28 @@ func TestErrorWrapperFailure(t *testing.T) {
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
t.Fatal("expected a nil conn here")
|
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) {
|
func errorWrapperCheckErr(t *testing.T, err error, op string) {
|
||||||
if !errors.Is(err, io.EOF) {
|
if !errors.Is(err, io.EOF) {
|
||||||
t.Fatal("expected another error here")
|
t.Fatal("expected another error here")
|
||||||
}
|
}
|
||||||
var errWrapper *errorsx.ErrWrapper
|
var errWrapper *ErrWrapper
|
||||||
if !errors.As(err, &errWrapper) {
|
if !errors.As(err, &errWrapper) {
|
||||||
t.Fatal("cannot cast to ErrWrapper")
|
t.Fatal("cannot cast to ErrWrapper")
|
||||||
}
|
}
|
||||||
if errWrapper.Operation != op {
|
if errWrapper.Operation != op {
|
||||||
t.Fatal("unexpected Operation")
|
t.Fatal("unexpected Operation")
|
||||||
}
|
}
|
||||||
if errWrapper.Failure != errorsx.FailureEOFError {
|
if errWrapper.Failure != FailureEOFError {
|
||||||
t.Fatal("unexpected failure")
|
t.Fatal("unexpected failure")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorWrapperSuccess(t *testing.T) {
|
func TestErrorWrapperSuccess(t *testing.T) {
|
||||||
ctx := context.Background()
|
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) {
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||||
return &netxmocks.Conn{
|
return &netxmocks.Conn{
|
||||||
MockRead: func(b []byte) (int, error) {
|
MockRead: func(b []byte) (int, error) {
|
||||||
|
@ -69,11 +68,11 @@ func TestErrorWrapperSuccess(t *testing.T) {
|
||||||
t.Fatal("expected non-nil conn here")
|
t.Fatal("expected non-nil conn here")
|
||||||
}
|
}
|
||||||
count, err := conn.Read(nil)
|
count, err := conn.Read(nil)
|
||||||
errorWrapperCheckIOResult(t, count, err, errorsx.ReadOperation)
|
errorWrapperCheckIOResult(t, count, err, ReadOperation)
|
||||||
count, err = conn.Write(nil)
|
count, err = conn.Write(nil)
|
||||||
errorWrapperCheckIOResult(t, count, err, errorsx.WriteOperation)
|
errorWrapperCheckIOResult(t, count, err, WriteOperation)
|
||||||
err = conn.Close()
|
err = conn.Close()
|
||||||
errorWrapperCheckErr(t, err, errorsx.CloseOperation)
|
errorWrapperCheckErr(t, err, CloseOperation)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorWrapperCheckIOResult(t *testing.T, count int, err error, op string) {
|
func errorWrapperCheckIOResult(t *testing.T, count int, err error, op string) {
|
Loading…
Reference in New Issue
Block a user