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 11:35:00 +02:00
|
|
|
// ErrorWrapperDialer is a dialer that performs error wrapping.
|
2021-07-01 17:15:44 +02:00
|
|
|
type ErrorWrapperDialer struct {
|
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)
|
2021-07-01 17:15:44 +02:00
|
|
|
err = SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 17:15:44 +02:00
|
|
|
Operation: ConnectOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-02-02 12:05:47 +01:00
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Read implements net.Conn.Read.
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Read(b []byte) (n int, err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
n, err = c.Conn.Read(b)
|
2021-07-01 17:15:44 +02:00
|
|
|
err = SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 17:15:44 +02:00
|
|
|
Operation: ReadOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Write implements net.Conn.Write.
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Write(b []byte) (n int, err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
n, err = c.Conn.Write(b)
|
2021-07-01 17:15:44 +02:00
|
|
|
err = SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 17:15:44 +02:00
|
|
|
Operation: WriteOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-02 11:35:00 +02:00
|
|
|
// Close implements net.Conn.Close.
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Close() (err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
err = c.Conn.Close()
|
2021-07-01 17:15:44 +02:00
|
|
|
err = SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 17:15:44 +02:00
|
|
|
Operation: CloseOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|