@@ -35,7 +35,15 @@ type DialOptions struct {
3535var secWebSocketKey = base64 .StdEncoding .EncodeToString (make ([]byte , 16 ))
3636
3737// Dial performs a WebSocket handshake on the given url with the given options.
38- func Dial (ctx context.Context , u string , opts DialOptions ) (_ * Conn , _ * http.Response , err error ) {
38+ func Dial (ctx context.Context , u string , opts DialOptions ) (* Conn , * http.Response , error ) {
39+ c , r , err := dial (ctx , u , opts )
40+ if err != nil {
41+ return nil , r , xerrors .Errorf ("failed to websocket dial: %w" , err )
42+ }
43+ return c , r , nil
44+ }
45+
46+ func dial (ctx context.Context , u string , opts DialOptions ) (_ * Conn , _ * http.Response , err error ) {
3947 if opts .HTTPClient == nil {
4048 opts .HTTPClient = http .DefaultClient
4149 }
@@ -45,7 +53,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
4553
4654 parsedURL , err := url .Parse (u )
4755 if err != nil {
48- return nil , nil , xerrors .Errorf ("failed to parse websocket url: %w" , err )
56+ return nil , nil , xerrors .Errorf ("failed to parse url: %w" , err )
4957 }
5058
5159 switch parsedURL .Scheme {
@@ -54,7 +62,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
5462 case "wss" :
5563 parsedURL .Scheme = "https"
5664 default :
57- return nil , nil , xerrors .Errorf ("unknown scheme in url : %q" , parsedURL .Scheme )
65+ return nil , nil , xerrors .Errorf ("unexpected url scheme scheme : %q" , parsedURL .Scheme )
5866 }
5967
6068 req , _ := http .NewRequest ("GET" , parsedURL .String (), nil )
@@ -70,7 +78,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
7078
7179 resp , err := opts .HTTPClient .Do (req )
7280 if err != nil {
73- return nil , nil , err
81+ return nil , nil , xerrors . Errorf ( "failed to send handshake request: %w" , err )
7482 }
7583 defer func () {
7684 respBody := resp .Body
@@ -90,7 +98,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
9098
9199 rwc , ok := resp .Body .(io.ReadWriteCloser )
92100 if ! ok {
93- return nil , resp , xerrors .Errorf ("websocket: body is not a read write closer but should be : %T" , rwc )
101+ return nil , resp , xerrors .Errorf ("response body is not a read write closer: %T" , rwc )
94102 }
95103
96104 // TODO pool bufio
@@ -108,15 +116,15 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
108116
109117func verifyServerResponse (resp * http.Response ) error {
110118 if resp .StatusCode != http .StatusSwitchingProtocols {
111- return xerrors .Errorf ("websocket: expected status code %v but got %v" , http .StatusSwitchingProtocols , resp .StatusCode )
119+ return xerrors .Errorf ("expected handshake response status code %v but got %v" , http .StatusSwitchingProtocols , resp .StatusCode )
112120 }
113121
114122 if ! headerValuesContainsToken (resp .Header , "Connection" , "Upgrade" ) {
115- return xerrors .Errorf ("websocket: protocol violation: Connection header does not contain Upgrade: %q" , resp .Header .Get ("Connection" ))
123+ return xerrors .Errorf ("websocket protocol violation: Connection header does not contain Upgrade: %q" , resp .Header .Get ("Connection" ))
116124 }
117125
118126 if ! headerValuesContainsToken (resp .Header , "Upgrade" , "WebSocket" ) {
119- return xerrors .Errorf ("websocket: protocol violation: Upgrade header does not contain websocket: %q" , resp .Header .Get ("Upgrade" ))
127+ return xerrors .Errorf ("websocket protocol violation: Upgrade header does not contain websocket: %q" , resp .Header .Get ("Upgrade" ))
120128 }
121129
122130 // We do not care about Sec-WebSocket-Accept because it does not matter.
0 commit comments