2021-07-01 17:15:44 +02:00
|
|
|
package errorsx
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2021-07-01 17:15:44 +02:00
|
|
|
// Dialer establishes network connections.
|
|
|
|
type Dialer interface {
|
|
|
|
// DialContext behaves like net.Dialer.DialContext.
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
2021-07-02 14:00:46 +02:00
|
|
|
// ErrorWrapperDialer is a dialer that performs error wrapping. The connection
|
|
|
|
// returned by the DialContext function will also perform error wrapping.
|
2021-07-01 17:15:44 +02:00
|
|
|
type ErrorWrapperDialer struct {
|
2021-07-02 14:00:46 +02:00
|
|
|
// Dialer is the underlying dialer.
|
2021-02-02 12:05:47 +01:00
|
|
|
Dialer
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// DialContext implements Dialer.DialContext.
|
2021-07-01 17:15:44 +02:00
|
|
|
func (d *ErrorWrapperDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
|
|
|
if err != nil {
|
2021-07-02 14:00:46 +02:00
|
|
|
return nil, &ErrWrapper{
|
|
|
|
Failure: toFailureString(err),
|
|
|
|
Operation: ConnectOperation,
|
|
|
|
WrappedErr: err,
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
return &errorWrapperConn{Conn: conn}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// errorWrapperConn is a net.Conn that performs error wrapping.
|
|
|
|
type errorWrapperConn struct {
|
2021-07-02 14:00:46 +02:00
|
|
|
// Conn is the underlying connection.
|
2021-02-02 12:05:47 +01:00
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Read implements net.Conn.Read.
|
2021-07-02 14:00:46 +02:00
|
|
|
func (c *errorWrapperConn) Read(b []byte) (int, error) {
|
|
|
|
count, err := c.Conn.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, &ErrWrapper{
|
|
|
|
Failure: toFailureString(err),
|
|
|
|
Operation: ReadOperation,
|
|
|
|
WrappedErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Write implements net.Conn.Write.
|
2021-07-02 14:00:46 +02:00
|
|
|
func (c *errorWrapperConn) Write(b []byte) (int, error) {
|
|
|
|
count, err := c.Conn.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, &ErrWrapper{
|
|
|
|
Failure: toFailureString(err),
|
|
|
|
Operation: WriteOperation,
|
|
|
|
WrappedErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Close implements net.Conn.Close.
|
2021-07-02 14:00:46 +02:00
|
|
|
func (c *errorWrapperConn) Close() error {
|
|
|
|
err := c.Conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return &ErrWrapper{
|
|
|
|
Failure: toFailureString(err),
|
|
|
|
Operation: CloseOperation,
|
|
|
|
WrappedErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|