expose a VersionNegoationError

This commit is contained in:
Marten Seemann
2021-04-25 19:03:34 +07:00
parent 42b61729bd
commit 1ce572228b
5 changed files with 47 additions and 24 deletions

View File

@@ -2,6 +2,8 @@ package qerr
import (
"fmt"
"github.com/lucas-clemente/quic-go/internal/protocol"
)
var (
@@ -92,3 +94,18 @@ func (e *HandshakeTimeoutError) Is(target error) bool {
_, ok := target.(*HandshakeTimeoutError)
return ok
}
// A VersionNegotiationError occurs when the client and the server can't agree on a QUIC version.
type VersionNegotiationError struct {
Ours []protocol.VersionNumber
Theirs []protocol.VersionNumber
}
func (e *VersionNegotiationError) Error() string {
return fmt.Sprintf("no compatible QUIC version found (we support %s, server offered %s)", e.Ours, e.Theirs)
}
func (e *VersionNegotiationError) Is(target error) bool {
_, ok := target.(*VersionNegotiationError)
return ok
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"net"
"github.com/lucas-clemente/quic-go/internal/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -101,4 +102,17 @@ var _ = Describe("QUIC Errors", func() {
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},
Theirs: []protocol.VersionNumber{4, 5, 6},
}).Error()).To(Equal("no compatible QUIC version found (we support [0x2 0x3], server offered [0x4 0x5 0x6])"))
})
})
})