forked from quic-go/quic-go
fix incorrect usage of errors.Is
errors.Is is supposed to used for equality of errors, not for type assertions. That's what errors.As is there for.
This commit is contained in:
@@ -6,50 +6,9 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
func (e *TransportError) Is(target error) bool {
|
||||
_, ok := target.(*TransportError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
|
||||
func (e *ApplicationError) Is(target error) bool {
|
||||
_, ok := target.(*ApplicationError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
|
||||
func (e *IdleTimeoutError) Is(target error) bool {
|
||||
_, ok := target.(*IdleTimeoutError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
|
||||
func (e *HandshakeTimeoutError) Is(target error) bool {
|
||||
_, ok := target.(*HandshakeTimeoutError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
|
||||
func (e *VersionNegotiationError) Is(target error) bool {
|
||||
_, ok := target.(*VersionNegotiationError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
|
||||
func (e *StatelessResetError) Is(target error) bool {
|
||||
_, ok := target.(*StatelessResetError)
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
return target == net.ErrClosed
|
||||
}
|
||||
func (e *TransportError) Is(target error) bool { return target == net.ErrClosed }
|
||||
func (e *ApplicationError) Is(target error) bool { return target == net.ErrClosed }
|
||||
func (e *IdleTimeoutError) Is(target error) bool { return target == net.ErrClosed }
|
||||
func (e *HandshakeTimeoutError) Is(target error) bool { return target == net.ErrClosed }
|
||||
func (e *VersionNegotiationError) Is(target error) bool { return target == net.ErrClosed }
|
||||
func (e *StatelessResetError) Is(target error) bool { return target == net.ErrClosed }
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// +build !go1.16
|
||||
|
||||
package qerr
|
||||
|
||||
func (e *TransportError) Is(target error) bool {
|
||||
_, ok := target.(*TransportError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *ApplicationError) Is(target error) bool {
|
||||
_, ok := target.(*ApplicationError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *IdleTimeoutError) Is(target error) bool {
|
||||
_, ok := target.(*IdleTimeoutError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *HandshakeTimeoutError) Is(target error) bool {
|
||||
_, ok := target.(*HandshakeTimeoutError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *VersionNegotiationError) Is(target error) bool {
|
||||
_, ok := target.(*VersionNegotiationError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e *StatelessResetError) Is(target error) bool {
|
||||
_, ok := target.(*StatelessResetError)
|
||||
return ok
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package qerr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
@@ -37,11 +36,6 @@ var _ = Describe("QUIC Errors", func() {
|
||||
}).Error()).To(Equal("FLOW_CONTROL_ERROR (frame type: 0x1337): foobar"))
|
||||
})
|
||||
|
||||
It("works with error assertions", func() {
|
||||
Expect(errors.Is(&TransportError{ErrorCode: FlowControlError}, &TransportError{})).To(BeTrue())
|
||||
Expect(errors.Is(&TransportError{ErrorCode: FlowControlError}, &ApplicationError{})).To(BeFalse())
|
||||
})
|
||||
|
||||
Context("crypto errors", func() {
|
||||
It("has a string representation for errors with a message", func() {
|
||||
err := NewCryptoError(0x42, "foobar")
|
||||
@@ -68,11 +62,6 @@ var _ = Describe("QUIC Errors", func() {
|
||||
ErrorCode: 0x42,
|
||||
}).Error()).To(Equal("Application error 0x42"))
|
||||
})
|
||||
|
||||
It("works with error assertions", func() {
|
||||
Expect(errors.Is(&ApplicationError{ErrorCode: 0x1234}, &ApplicationError{})).To(BeTrue())
|
||||
Expect(errors.Is(&ApplicationError{ErrorCode: 0x1234}, &TransportError{})).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Context("timeout errors", func() {
|
||||
@@ -85,8 +74,6 @@ var _ = Describe("QUIC Errors", func() {
|
||||
Expect(nerr.Timeout()).To(BeTrue())
|
||||
Expect(nerr.Temporary()).To(BeFalse())
|
||||
Expect(err.Error()).To(Equal("timeout: handshake did not complete in time"))
|
||||
Expect(errors.Is(err, &HandshakeTimeoutError{})).To(BeTrue())
|
||||
Expect(errors.Is(err, &IdleTimeoutError{})).To(BeFalse())
|
||||
})
|
||||
|
||||
It("idle timeouts", func() {
|
||||
@@ -98,16 +85,10 @@ var _ = Describe("QUIC Errors", func() {
|
||||
Expect(nerr.Timeout()).To(BeTrue())
|
||||
Expect(nerr.Temporary()).To(BeFalse())
|
||||
Expect(err.Error()).To(Equal("timeout: no recent network activity"))
|
||||
Expect(errors.Is(err, &HandshakeTimeoutError{})).To(BeFalse())
|
||||
Expect(errors.Is(err, &IdleTimeoutError{})).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
Context("Version Negotiation errors", func() {
|
||||
It("is a Version Negotiation error", func() {
|
||||
Expect(errors.Is(&VersionNegotiationError{Ours: []protocol.VersionNumber{2, 3}}, &VersionNegotiationError{})).To(BeTrue())
|
||||
})
|
||||
|
||||
It("has a string representation", func() {
|
||||
Expect((&VersionNegotiationError{
|
||||
Ours: []protocol.VersionNumber{2, 3},
|
||||
@@ -119,10 +100,6 @@ var _ = Describe("QUIC Errors", func() {
|
||||
Context("Stateless Reset errors", func() {
|
||||
token := protocol.StatelessResetToken{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
|
||||
|
||||
It("is a Stateless Reset error", func() {
|
||||
Expect(errors.Is(&StatelessResetError{Token: token}, &StatelessResetError{})).To(BeTrue())
|
||||
})
|
||||
|
||||
It("has a string representation", func() {
|
||||
Expect((&StatelessResetError{Token: token}).Error()).To(Equal("received a stateless reset with token 000102030405060708090a0b0c0d0e0f"))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user