use the error names from the draft

This commit is contained in:
Marten Seemann
2019-03-06 13:39:38 +09:00
parent 9c09e84765
commit 662041649f
11 changed files with 64 additions and 59 deletions

View File

@@ -1,11 +1,11 @@
package qerr
// The error codes defined by QUIC
// Remember to run `go generate ./...` whenever the error codes change.
// This uses the Go stringer tool, which can be installed by running
// go get -u golang.org/x/tools/cmd/stringer
import "fmt"
//go:generate stringer -type=ErrorCode
// ErrorCode can be used as a normal error without reason.
type ErrorCode uint16
// The error codes defined by QUIC
const (
NoError ErrorCode = 0x0
InternalError ErrorCode = 0x1
@@ -21,3 +21,40 @@ const (
InvalidMigration ErrorCode = 0xc
CryptoError ErrorCode = 0x100
)
func (e ErrorCode) Error() string {
return e.String()
}
func (e ErrorCode) String() string {
switch e {
case NoError:
return "NO_ERROR"
case InternalError:
return "INTERNAL_ERROR"
case ServerBusy:
return "SERVER_BUSY"
case FlowControlError:
return "FLOW_CONTROL_ERROR"
case StreamLimitError:
return "STREAM_LIMIT_ERROR"
case StreamStateError:
return "STREAM_STATE_ERROR"
case FinalSizeError:
return "FINAL_SIZE_ERROR"
case FrameEncodingError:
return "FRAME_ENCODING_ERROR"
case TransportParameterError:
return "TRANSPORT_PARAMETER_ERROR"
case VersionNegotiationError:
return "VERSION_NEGOTIATION_ERROR"
case ProtocolViolation:
return "PROTOCOL_VIOLATION"
case InvalidMigration:
return "INVALID_MIGRATION"
case CryptoError:
return "CRYPTO_ERROR"
default:
return fmt.Sprintf("unknown error code: %d", e)
}
}

View File

@@ -1,28 +0,0 @@
// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT.
package qerr
import "strconv"
const (
_ErrorCode_name_0 = "NoErrorInternalErrorServerBusyFlowControlErrorStreamLimitErrorStreamStateErrorFinalSizeErrorFrameEncodingErrorTransportParameterErrorVersionNegotiationErrorProtocolViolation"
_ErrorCode_name_1 = "InvalidMigration"
_ErrorCode_name_2 = "CryptoError"
)
var (
_ErrorCode_index_0 = [...]uint8{0, 7, 20, 30, 46, 62, 78, 92, 110, 133, 156, 173}
)
func (i ErrorCode) String() string {
switch {
case 0 <= i && i <= 10:
return _ErrorCode_name_0[_ErrorCode_index_0[i]:_ErrorCode_index_0[i+1]]
case i == 12:
return _ErrorCode_name_1
case i == 256:
return _ErrorCode_name_2
default:
return "ErrorCode(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

View File

@@ -24,14 +24,17 @@ var _ = Describe("error codes", func() {
filename := path.Join(path.Dir(thisfile), "error_codes.go")
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
Expect(err).NotTo(HaveOccurred())
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
constSpecs := fileAst.Decls[2].(*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.ParseInt(valString, 0, 64)
Expect(err).NotTo(HaveOccurred())
Expect(ErrorCode(val).String()).To(Equal(name))
Expect(ErrorCode(val).String()).ToNot(Equal("unknown error code"))
}
})
It("has a string representation for unknown error codes", func() {
Expect(ErrorCode(1337).String()).To(Equal("unknown error code: 1337"))
})
})

View File

@@ -5,13 +5,6 @@ import (
"net"
)
// ErrorCode can be used as a normal error without reason.
type ErrorCode uint16
func (e ErrorCode) Error() string {
return e.String()
}
// A QuicError consists of an error code plus a error reason
type QuicError struct {
ErrorCode ErrorCode

View File

@@ -11,14 +11,14 @@ var _ = Describe("QUIC Transport Errors", func() {
Context("QuicError", func() {
It("has a string representation", func() {
err := Error(FlowControlError, "foobar")
Expect(err.Error()).To(Equal("FlowControlError: foobar"))
Expect(err.Error()).To(Equal("FLOW_CONTROL_ERROR: foobar"))
})
})
Context("ErrorCode", func() {
It("works as error", func() {
var err error = StreamStateError
Expect(err).To(MatchError("StreamStateError"))
Expect(err).To(MatchError("STREAM_STATE_ERROR"))
})
})