Skip to content

Commit

Permalink
review: use errors.As for struct error types in wsconn.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ukane-philemon committed Feb 16, 2023
1 parent a88f34b commit b792587
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions client/comms/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ func (cs ConnectionStatus) String() string {
// caused by or related to an invalid cert.
var invalidCertRegexp = regexp.MustCompile(".*(unknown authority|not standards compliant|not trusted)")

// IsErrorInvalidCert checks if the provided error is one of the different
// isErrorInvalidCert checks if the provided error is one of the different
// variant of an invalid cert error returned from the x509 package or is
// ErrInvalidCert.
func IsErrorInvalidCert(err error) bool {
var invalidCert x509.CertificateInvalidError
var unknownCertAuth x509.UnknownAuthorityError
return errors.Is(err, ErrInvalidCert) || errors.Is(err, invalidCert) ||
errors.Is(err, unknownCertAuth) || invalidCertRegexp.MatchString(err.Error())
func isErrorInvalidCert(err error) bool {
var invalidCertErr x509.CertificateInvalidError
var unknownCertAuthErr x509.UnknownAuthorityError
var hostNameErr x509.HostnameError
return errors.Is(err, ErrInvalidCert) || errors.As(err, &invalidCertErr) || errors.As(err, &hostNameErr) ||
errors.As(err, &unknownCertAuthErr) || invalidCertRegexp.MatchString(err.Error())
}

// ErrInvalidCert is the error returned when attempting to use an invalid cert
Expand Down Expand Up @@ -227,8 +228,7 @@ func (conn *wsConn) connect(ctx context.Context) error {
}
ws, _, err := dialer.DialContext(ctx, conn.cfg.URL, nil)
if err != nil {
var e x509.HostnameError // No need to retry...
if IsErrorInvalidCert(err) || errors.Is(err, e) {
if isErrorInvalidCert(err) {
conn.setConnectionStatus(InvalidCert)
if conn.tlsCfg == nil {
return ErrCertRequired
Expand Down

0 comments on commit b792587

Please sign in to comment.