create a QuicError type

This commit is contained in:
Marten Seemann
2016-04-19 15:54:18 +07:00
parent 600d3805a2
commit 90cb792477
3 changed files with 34 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import "github.com/lucas-clemente/quic-go/protocol"
const (
QUIC_NO_ERROR = protocol.ErrorCode(0)
QUIC_INTERNAL_ERROR = protocol.ErrorCode(1)
QUIC_STREAM_DATA_AFTER_TERMINATION = protocol.ErrorCode(2)
// QUIC_SERVER_ERROR_PROCESSING_STREAM= There was some server error which halted stream processing.
// QUIC_MULTIPLE_TERMINATION_OFFSETS= The sender received two mismatching fin or reset offsets for a single stream.

21
protocol/quic_error.go Normal file
View File

@@ -0,0 +1,21 @@
package protocol
// A QuicError is a QUIC error
type QuicError struct {
ErrorCode ErrorCode
ErrorMessage string
}
// NewQuicError creates a new Quic Error
func NewQuicError(errorCode ErrorCode, errorMessage string) *QuicError {
return &QuicError{
ErrorCode: errorCode,
ErrorMessage: errorMessage,
}
}
func (e *QuicError) Error() string {
return e.ErrorMessage
}
var _ error = &QuicError{}

View File

@@ -138,10 +138,10 @@ func (s *Session) HandlePacket(addr *net.UDPAddr, publicHeaderBinary []byte, pub
// PING, do nothing
r.ReadByte()
default:
err = fmt.Errorf("unknown frame type: %x", typeByte)
err = protocol.NewQuicError(errorcodes.QUIC_INVALID_FRAME_DATA, fmt.Sprintf("unknown type byte 0x%x", typeByte))
}
if err != nil {
s.Close(errorcodes.QUIC_INVALID_FRAME_DATA)
s.Close(err)
return err
}
}
@@ -231,9 +231,17 @@ func (s *Session) handleRstStreamFrame(r *bytes.Reader) error {
}
// Close closes the connection by sending a ConnectionClose frame
func (s *Session) Close(errorCode protocol.ErrorCode) error {
func (s *Session) Close(e error) error {
errorCode := protocol.ErrorCode(1)
reasonPhrase := e.Error()
quicError, ok := e.(*protocol.QuicError)
if ok {
errorCode = quicError.ErrorCode
}
frame := &frames.ConnectionCloseFrame{
ErrorCode: errorCode,
ErrorCode: errorCode,
ReasonPhrase: reasonPhrase,
}
return s.SendFrame(frame)
}