move all error things to new qerr package, replacing errorcodes

This commit is contained in:
Lucas Clemente
2016-05-17 10:24:34 +02:00
parent 62da35d613
commit bfaa4200df
12 changed files with 71 additions and 66 deletions

View File

@@ -1,4 +0,0 @@
package errorcodes
// An ErrorCode in QUIC
type ErrorCode uint32

View File

@@ -6,14 +6,14 @@ import (
"io"
"math"
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
"github.com/lucas-clemente/quic-go/utils"
)
// A ConnectionCloseFrame in QUIC
type ConnectionCloseFrame struct {
ErrorCode errorcodes.ErrorCode
ErrorCode qerr.ErrorCode
ReasonPhrase string
}
@@ -31,7 +31,7 @@ func ParseConnectionCloseFrame(r *bytes.Reader) (*ConnectionCloseFrame, error) {
if err != nil {
return nil, err
}
frame.ErrorCode = errorcodes.ErrorCode(errorCode)
frame.ErrorCode = qerr.ErrorCode(errorCode)
reasonPhraseLen, err := utils.ReadUint16(r)
if err != nil {

View File

@@ -3,8 +3,8 @@ package frames
import (
"bytes"
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@@ -15,7 +15,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
b := bytes.NewReader([]byte{0x40, 0x19, 0x00, 0x00, 0x00, 0x1B, 0x00, 0x4e, 0x6f, 0x20, 0x72, 0x65, 0x63, 0x65, 0x6e, 0x74, 0x20, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x61, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 0x2e})
frame, err := ParseConnectionCloseFrame(b)
Expect(err).ToNot(HaveOccurred())
Expect(frame.ErrorCode).To(Equal(errorcodes.ErrorCode(0x19)))
Expect(frame.ErrorCode).To(Equal(qerr.ErrorCode(0x19)))
Expect(frame.ReasonPhrase).To(Equal("No recent network activity."))
Expect(b.Len()).To(Equal(0))
})
@@ -24,7 +24,7 @@ var _ = Describe("ConnectionCloseFrame", func() {
b := bytes.NewReader([]byte{0x02, 0xAD, 0xFB, 0xCA, 0xDE, 0x00, 0x00})
frame, err := ParseConnectionCloseFrame(b)
Expect(err).ToNot(HaveOccurred())
Expect(frame.ErrorCode).To(Equal(errorcodes.ErrorCode(0xDECAFBAD)))
Expect(frame.ErrorCode).To(Equal(qerr.ErrorCode(0xDECAFBAD)))
Expect(frame.ReasonPhrase).To(BeEmpty())
Expect(b.Len()).To(Equal(0))
})

View File

@@ -7,9 +7,9 @@ import (
"io/ioutil"
"github.com/lucas-clemente/quic-go/crypto"
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
)
type unpackedPacket struct {
@@ -27,7 +27,7 @@ func (u *packetUnpacker) Unpack(publicHeaderBinary []byte, hdr *publicHeader, r
plaintext, err := u.aead.Open(hdr.PacketNumber, publicHeaderBinary, ciphertext)
if err != nil {
// Wrap err in quicError so that public reset is sent by session
return nil, protocol.Error(errorcodes.DecryptionFailure, err.Error())
return nil, qerr.Error(qerr.DecryptionFailure, err.Error())
}
r = bytes.NewReader(plaintext)
@@ -71,7 +71,7 @@ ReadLoop:
case 0x07:
frame, err = frames.ParsePingFrame(r)
default:
err = protocol.Error(errorcodes.InvalidFrameData, fmt.Sprintf("unknown type byte 0x%x", typeByte))
err = qerr.Error(qerr.InvalidFrameData, fmt.Sprintf("unknown type byte 0x%x", typeByte))
}
}
if err != nil {

View File

@@ -1,27 +0,0 @@
package protocol
import (
"fmt"
"github.com/lucas-clemente/quic-go/errorcodes"
)
// A QuicError is a QUIC error
type QuicError struct {
ErrorCode errorcodes.ErrorCode
ErrorMessage string
}
// Error creates a new Quic Error
func Error(errorCode errorcodes.ErrorCode, errorMessage string) *QuicError {
return &QuicError{
ErrorCode: errorCode,
ErrorMessage: errorMessage,
}
}
func (e *QuicError) Error() string {
return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage)
}
var _ error = &QuicError{}

View File

@@ -1,4 +1,4 @@
package errorcodes
package qerr
// The error codes defined by QUIC
// Remeber to run `go generate ./...` whenever the error codes change.

View File

@@ -1,6 +1,6 @@
// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT
package errorcodes
package qerr
import "fmt"

View File

@@ -1,4 +1,4 @@
package errorcodes_test
package qerr_test
import (
"go/ast"
@@ -7,24 +7,18 @@ import (
"os"
"strconv"
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestErrorcodes(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Errorcodes Suite")
}
var _ = Describe("error codes", func() {
// If this test breaks, you should run `go generate ./...`
It("has a string representation for every error code", func() {
// We parse the error code file, extract all constants, and verify that
// each of them has a string version. Go FTW!
filename := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/errorcodes/error_codes.go"
filename := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/qerr/error_codes.go"
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
Expect(err).NotTo(HaveOccurred())
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
@@ -34,7 +28,7 @@ var _ = Describe("error codes", func() {
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.Atoi(valString)
Expect(err).NotTo(HaveOccurred())
Expect(errorcodes.ErrorCode(val).String()).To(Equal(name))
Expect(qerr.ErrorCode(val).String()).To(Equal(name))
}
})
})

13
qerr/errors_suite_test.go Normal file
View File

@@ -0,0 +1,13 @@
package qerr_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestErrorcodes(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Errors Suite")
}

30
qerr/quic_error.go Normal file
View File

@@ -0,0 +1,30 @@
package qerr
import (
"fmt"
)
// ErrorCode can be used as a normal error without reason.
type ErrorCode uint32
func (e ErrorCode) Error() string {
return e.String()
}
// A QuicError consists of an error code plus a error reason
type QuicError struct {
ErrorCode ErrorCode
ErrorMessage string
}
// Error creates a new QuicError instance
func Error(errorCode ErrorCode, errorMessage string) *QuicError {
return &QuicError{
ErrorCode: errorCode,
ErrorMessage: errorMessage,
}
}
func (e *QuicError) Error() string {
return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage)
}

View File

@@ -1,8 +1,7 @@
package protocol_test
package qerr_test
import (
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -10,7 +9,7 @@ import (
var _ = Describe("Quic error", func() {
It("has a string representation", func() {
err := protocol.Error(errorcodes.InternalError, "foobar")
err := qerr.Error(qerr.InternalError, "foobar")
Expect(err.Error()).To(Equal("InternalError: foobar"))
})
})

View File

@@ -9,10 +9,10 @@ import (
"github.com/lucas-clemente/quic-go/ackhandler"
"github.com/lucas-clemente/quic-go/congestion"
"github.com/lucas-clemente/quic-go/errorcodes"
"github.com/lucas-clemente/quic-go/frames"
"github.com/lucas-clemente/quic-go/handshake"
"github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr"
"github.com/lucas-clemente/quic-go/utils"
)
@@ -149,7 +149,7 @@ func (s *Session) run() {
return
case p := <-s.receivedPackets:
err = s.handlePacketImpl(p.remoteAddr, p.publicHeader, p.data)
if qErr, ok := err.(*protocol.QuicError); ok && qErr.ErrorCode == errorcodes.DecryptionFailure {
if qErr, ok := err.(*qerr.QuicError); ok && qErr.ErrorCode == qerr.DecryptionFailure {
s.tryQueueingUndecryptablePacket(p)
continue
}
@@ -161,7 +161,7 @@ func (s *Session) run() {
case <-s.aeadChanged:
s.tryDecryptingQueuedPackets()
case <-time.After(s.connectionParametersManager.GetIdleConnectionStateLifetime()):
s.Close(protocol.Error(errorcodes.NetworkIdleTimeout, "No recent network activity."), true)
s.Close(qerr.Error(qerr.NetworkIdleTimeout, "No recent network activity."), true)
}
if err != nil {
@@ -363,24 +363,24 @@ func (s *Session) Close(e error, sendConnectionClose bool) error {
}
if e == nil {
e = protocol.Error(errorcodes.PeerGoingAway, "peer going away")
e = qerr.Error(qerr.PeerGoingAway, "peer going away")
}
utils.Errorf("Closing session with error: %s", e.Error())
// if e is a QUIC error, send it to the client
// else, send the generic QUIC internal error
var errorCode errorcodes.ErrorCode
var errorCode qerr.ErrorCode
var reasonPhrase string
quicError, ok := e.(*protocol.QuicError)
quicError, ok := e.(*qerr.QuicError)
if ok {
errorCode = quicError.ErrorCode
reasonPhrase = e.Error()
} else {
errorCode = errorcodes.InternalError
errorCode = qerr.InternalError
}
s.closeStreamsWithError(e)
if errorCode == errorcodes.DecryptionFailure {
if errorCode == qerr.DecryptionFailure {
return s.sendPublicReset(s.lastRcvdPacketNumber)
}
@@ -621,7 +621,7 @@ func (s *Session) congestionAllowsSending() bool {
func (s *Session) tryQueueingUndecryptablePacket(p receivedPacket) {
utils.Debugf("Queueing packet 0x%x for later decryption", p.publicHeader.PacketNumber)
if len(s.undecryptablePackets)+1 >= protocol.MaxUndecryptablePackets {
s.Close(protocol.Error(errorcodes.DecryptionFailure, "too many undecryptable packets received"), true)
s.Close(qerr.Error(qerr.DecryptionFailure, "too many undecryptable packets received"), true)
}
s.undecryptablePackets = append(s.undecryptablePackets, p)
}