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.

Commit d673079

Browse files
authored
Add err wrapping to dialer (#402)
1 parent 961f7dd commit d673079

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

wsnet/dial.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
104104
return nil, fmt.Errorf("create peer connection: %w", err)
105105
}
106106
log.Debug(ctx, "created peer connection")
107+
defer func() {
108+
if err != nil {
109+
// Wrap our error with some extra details.
110+
err = errWrap{
111+
err: err,
112+
iceServers: rtc.GetConfiguration().ICEServers,
113+
rtc: rtc.ConnectionState(),
114+
}
115+
}
116+
}()
117+
107118
rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
108119
log.Debug(ctx, "connection state change", slog.F("state", pcs.String()))
109120
})
@@ -159,7 +170,9 @@ func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, er
159170
connClosers: []io.Closer{ctrl},
160171
}
161172

162-
return dialer, dialer.negotiate(ctx)
173+
// This is on a separate line so the defer above catches it.
174+
err = dialer.negotiate(ctx)
175+
return dialer, err
163176
}
164177

165178
// Dialer enables arbitrary dialing to any network and address

wsnet/error.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package wsnet
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/pion/webrtc/v3"
8+
)
9+
10+
// errWrap wraps the error with some extra details about the state of the
11+
// connection.
12+
type errWrap struct {
13+
err error
14+
15+
iceServers []webrtc.ICEServer
16+
rtc webrtc.PeerConnectionState
17+
}
18+
19+
var _ error = errWrap{}
20+
var _ interface{ Unwrap() error } = errWrap{}
21+
22+
// Error implements error.
23+
func (e errWrap) Error() string {
24+
return fmt.Sprintf("%v (ice: [%v], rtc: %v)", e.err.Error(), e.ice(), e.rtc.String())
25+
}
26+
27+
func (e errWrap) ice() string {
28+
msgs := []string{}
29+
for _, s := range e.iceServers {
30+
msgs = append(msgs, strings.Join(s.URLs, ", "))
31+
}
32+
33+
return strings.Join(msgs, ", ")
34+
}
35+
36+
// Unwrap implements Unwrapper.
37+
func (e errWrap) Unwrap() error {
38+
return e.err
39+
}

0 commit comments

Comments
 (0)