Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add err wrapping to dialer #402

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion wsnet/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
return nil, fmt.Errorf("create peer connection: %w", err)
}
log.Debug(ctx, "created peer connection")
defer func() {
if err != nil {
// Wrap our error with some extra details.
err = errWrap{
err: err,
iceServers: rtc.GetConfiguration().ICEServers,
rtc: rtc.ConnectionState(),
}
}
}()

rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
log.Debug(ctx, "connection state change", slog.F("state", pcs.String()))
})
Expand Down Expand Up @@ -159,7 +170,9 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
connClosers: []io.Closer{ctrl},
}

return dialer, dialer.negotiate(ctx)
// This is on a separate line so the defer above catches it.
err = dialer.negotiate(ctx)
return dialer, err
}

// Dialer enables arbitrary dialing to any network and address
Expand Down
39 changes: 39 additions & 0 deletions wsnet/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wsnet

import (
"fmt"
"strings"

"github.com/pion/webrtc/v3"
)

// errWrap wraps the error with some extra details about the state of the
// connection.
type errWrap struct {
err error

iceServers []webrtc.ICEServer
rtc webrtc.PeerConnectionState
}

var _ error = errWrap{}
var _ interface{ Unwrap() error } = errWrap{}

// Error implements error.
func (e errWrap) Error() string {
return fmt.Sprintf("%v (ice: [%v], rtc: %v)", e.err.Error(), e.ice(), e.rtc.String())
}

func (e errWrap) ice() string {
msgs := []string{}
for _, s := range e.iceServers {
msgs = append(msgs, strings.Join(s.URLs, ", "))
}

return strings.Join(msgs, ", ")
}

// Unwrap implements Unwrapper.
func (e errWrap) Unwrap() error {
return e.err
}