forked from quic-go/quic-go
move all error things to new qerr package, replacing errorcodes
This commit is contained in:
12
qerr/error_codes.go
Normal file
12
qerr/error_codes.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package qerr
|
||||
|
||||
// The error codes defined by QUIC
|
||||
// Remeber to run `go generate ./...` whenever the error codes change.
|
||||
//go:generate stringer -type=ErrorCode
|
||||
const (
|
||||
InternalError ErrorCode = 1
|
||||
InvalidFrameData ErrorCode = 4
|
||||
DecryptionFailure ErrorCode = 12
|
||||
PeerGoingAway ErrorCode = 16
|
||||
NetworkIdleTimeout ErrorCode = 25
|
||||
)
|
||||
38
qerr/errorcode_string.go
Normal file
38
qerr/errorcode_string.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT
|
||||
|
||||
package qerr
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
_ErrorCode_name_0 = "InternalError"
|
||||
_ErrorCode_name_1 = "InvalidFrameData"
|
||||
_ErrorCode_name_2 = "DecryptionFailure"
|
||||
_ErrorCode_name_3 = "PeerGoingAway"
|
||||
_ErrorCode_name_4 = "NetworkIdleTimeout"
|
||||
)
|
||||
|
||||
var (
|
||||
_ErrorCode_index_0 = [...]uint8{0, 13}
|
||||
_ErrorCode_index_1 = [...]uint8{0, 16}
|
||||
_ErrorCode_index_2 = [...]uint8{0, 17}
|
||||
_ErrorCode_index_3 = [...]uint8{0, 13}
|
||||
_ErrorCode_index_4 = [...]uint8{0, 18}
|
||||
)
|
||||
|
||||
func (i ErrorCode) String() string {
|
||||
switch {
|
||||
case i == 1:
|
||||
return _ErrorCode_name_0
|
||||
case i == 4:
|
||||
return _ErrorCode_name_1
|
||||
case i == 12:
|
||||
return _ErrorCode_name_2
|
||||
case i == 16:
|
||||
return _ErrorCode_name_3
|
||||
case i == 25:
|
||||
return _ErrorCode_name_4
|
||||
default:
|
||||
return fmt.Sprintf("ErrorCode(%d)", i)
|
||||
}
|
||||
}
|
||||
34
qerr/errorcodes_test.go
Normal file
34
qerr/errorcodes_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package qerr_test
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/qerr"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
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/qerr/error_codes.go"
|
||||
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
|
||||
Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
|
||||
for _, c := range constSpecs {
|
||||
name := c.(*ast.ValueSpec).Names[0].Name
|
||||
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
|
||||
val, err := strconv.Atoi(valString)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(qerr.ErrorCode(val).String()).To(Equal(name))
|
||||
}
|
||||
})
|
||||
})
|
||||
13
qerr/errors_suite_test.go
Normal file
13
qerr/errors_suite_test.go
Normal 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
30
qerr/quic_error.go
Normal 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)
|
||||
}
|
||||
15
qerr/quic_error_test.go
Normal file
15
qerr/quic_error_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package qerr_test
|
||||
|
||||
import (
|
||||
"github.com/lucas-clemente/quic-go/qerr"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Quic error", func() {
|
||||
It("has a string representation", func() {
|
||||
err := qerr.Error(qerr.InternalError, "foobar")
|
||||
Expect(err.Error()).To(Equal("InternalError: foobar"))
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user