refactor: move dialer's errorwrapper in i/errorsx (#417)

Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
Simone Basso 2021-07-01 17:15:44 +02:00 committed by GitHub
parent 72acd175a0
commit ceefcaf45e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 26 deletions

View File

@ -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}
}

View File

@ -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")
}

View File

@ -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
}

View File

@ -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) {