include the frame type in the QuicError error message

This commit is contained in:
Marten Seemann
2019-11-08 12:54:05 +07:00
parent 2c2b5da612
commit 76c742a43d
3 changed files with 41 additions and 4 deletions

View File

@@ -31,11 +31,20 @@ func (e ErrorCode) isCryptoError() bool {
func (e ErrorCode) Error() string { func (e ErrorCode) Error() string {
if e.isCryptoError() { if e.isCryptoError() {
return fmt.Sprintf("%s: %s", e.String(), qtls.Alert(e-0x100).Error()) return fmt.Sprintf("%s: %s", e.String(), e.Message())
} }
return e.String() return e.String()
} }
// Message is a description of the error.
// It only returns a non-empty string for crypto errors.
func (e ErrorCode) Message() string {
if !e.isCryptoError() {
return ""
}
return qtls.Alert(e - 0x100).Error()
}
func (e ErrorCode) String() string { func (e ErrorCode) String() string {
switch e { switch e {
case NoError: case NoError:

View File

@@ -8,6 +8,7 @@ import (
// A QuicError consists of an error code plus a error reason // A QuicError consists of an error code plus a error reason
type QuicError struct { type QuicError struct {
ErrorCode ErrorCode ErrorCode ErrorCode
FrameType uint64 // only valid if this not an application error
ErrorMessage string ErrorMessage string
isTimeout bool isTimeout bool
isApplicationError bool isApplicationError bool
@@ -23,6 +24,15 @@ func Error(errorCode ErrorCode, errorMessage string) *QuicError {
} }
} }
// ErrorWithFrameType creates a new QuicError instance for a specific frame type
func ErrorWithFrameType(errorCode ErrorCode, frameType uint64, errorMessage string) *QuicError {
return &QuicError{
ErrorCode: errorCode,
FrameType: frameType,
ErrorMessage: errorMessage,
}
}
// TimeoutError creates a new QuicError instance for a timeout error // TimeoutError creates a new QuicError instance for a timeout error
func TimeoutError(errorMessage string) *QuicError { func TimeoutError(errorMessage string) *QuicError {
return &QuicError{ return &QuicError{
@@ -55,10 +65,18 @@ func (e *QuicError) Error() string {
} }
return fmt.Sprintf("Application error %#x: %s", uint64(e.ErrorCode), e.ErrorMessage) return fmt.Sprintf("Application error %#x: %s", uint64(e.ErrorCode), e.ErrorMessage)
} }
if len(e.ErrorMessage) == 0 { str := e.ErrorCode.String()
return e.ErrorCode.Error() if e.FrameType != 0 {
str += fmt.Sprintf(" (frame type: %#x)", e.FrameType)
} }
return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage) msg := e.ErrorMessage
if len(msg) == 0 {
msg = e.ErrorCode.Message()
}
if len(msg) == 0 {
return str
}
return str + ": " + msg
} }
// IsCryptoError says if this error is a crypto error // IsCryptoError says if this error is a crypto error

View File

@@ -20,6 +20,16 @@ var _ = Describe("QUIC Transport Errors", func() {
Expect(err.Error()).To(Equal("FLOW_CONTROL_ERROR")) Expect(err.Error()).To(Equal("FLOW_CONTROL_ERROR"))
}) })
It("includes the frame type, for errors without a message", func() {
err := ErrorWithFrameType(FlowControlError, 0x1337, "")
Expect(err.Error()).To(Equal("FLOW_CONTROL_ERROR (frame type: 0x1337)"))
})
It("includes the frame type, for errors with a message", func() {
err := ErrorWithFrameType(FlowControlError, 0x1337, "foobar")
Expect(err.Error()).To(Equal("FLOW_CONTROL_ERROR (frame type: 0x1337): foobar"))
})
It("has a string representation for timeout errors", func() { It("has a string representation for timeout errors", func() {
err := TimeoutError("foobar") err := TimeoutError("foobar")
Expect(err.Timeout()).To(BeTrue()) Expect(err.Timeout()).To(BeTrue())