Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Feb 9, 2022
1 parent 352c1cb commit 0eb1a5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ Access-Control-Allow-Credentials: true
*/
```

And also you can force using `HTTP/2` if you want, will return error if server does not support:

```go
client := req.C().EnableForceHTTP2()
client.R().MustGet("https://baidu.com")
/* Output
panic: Get "https://baidu.com": server does not support http2, you can use http/1.1 which is supported
*/
```

## <a name="Param">URL Path and Query Parameter</a>

**Path Parameter**
Expand Down
12 changes: 10 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,11 +1500,19 @@ func (pconn *persistConn) addTLS(ctx context.Context, name string, trace *httptr
pconn.tlsState = &cs
pconn.conn = tlsConn
if pconn.t.ForceHttpVersion == HTTP2 && cs.NegotiatedProtocol != http2NextProtoTLS {
return fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", cs.NegotiatedProtocol, http2NextProtoTLS)
return newHttp2NotSupportedError(cs.NegotiatedProtocol)
}
return nil
}

func newHttp2NotSupportedError(negotiatedProtocol string) error {
errMsg := "server does not support http2"
if negotiatedProtocol != "" {
errMsg += fmt.Sprintf(", you can use %s which is supported", negotiatedProtocol)
}
return errors.New(errMsg)
}

type erringRoundTripper interface {
RoundTripErr() error
}
Expand Down Expand Up @@ -1552,7 +1560,7 @@ func (t *Transport) dialConn(ctx context.Context, cm connectMethod) (pconn *pers
}
pconn.tlsState = &cs
if pconn.t.ForceHttpVersion == HTTP2 && cs.NegotiatedProtocol != http2NextProtoTLS {
return nil, fmt.Errorf("http2: unexpected ALPN protocol %q; want %q", cs.NegotiatedProtocol, http2NextProtoTLS)
return nil, newHttp2NotSupportedError(cs.NegotiatedProtocol)
}
}
} else {
Expand Down

0 comments on commit 0eb1a5e

Please sign in to comment.