From f5238bf7b1ea8b42e7fb29535679dd712a613602 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 25 Apr 2021 18:28:25 +0700 Subject: [PATCH] move the ApplicationErrorCdoe to the qerr package --- integrationtests/self/http_test.go | 5 +++-- interface.go | 3 ++- internal/mocks/quic/early_session.go | 4 ++-- internal/mocks/quic/stream.go | 5 +++-- internal/protocol/protocol.go | 3 --- internal/qerr/quic_error.go | 5 ++++- internal/wire/reset_stream_frame.go | 5 +++-- internal/wire/reset_stream_frame_test.go | 3 ++- internal/wire/stop_sending_frame.go | 5 +++-- internal/wire/stop_sending_frame_test.go | 3 ++- packet_packer.go | 2 +- qlog/types_test.go | 2 +- receive_stream.go | 5 +++-- receive_stream_test.go | 5 +++-- send_stream.go | 6 +++--- send_stream_test.go | 5 +++-- session.go | 4 ++-- stream.go | 7 ++++--- 18 files changed, 44 insertions(+), 33 deletions(-) diff --git a/integrationtests/self/http_test.go b/integrationtests/self/http_test.go index 6e8da036..bf557991 100644 --- a/integrationtests/self/http_test.go +++ b/integrationtests/self/http_test.go @@ -14,9 +14,10 @@ import ( "strconv" "time" - quic "github.com/lucas-clemente/quic-go" + "github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go/http3" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/testdata" . "github.com/onsi/ginkgo" @@ -26,7 +27,7 @@ import ( type streamCancelError interface { Canceled() bool - ErrorCode() protocol.ApplicationErrorCode + ErrorCode() qerr.ApplicationErrorCode } var _ = Describe("HTTP tests", func() { diff --git a/interface.go b/interface.go index f29a1e77..b8fa4368 100644 --- a/interface.go +++ b/interface.go @@ -9,6 +9,7 @@ import ( "github.com/lucas-clemente/quic-go/internal/handshake" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/logging" ) @@ -66,7 +67,7 @@ type TokenStore interface { // An ErrorCode is an application-defined error code. // Valid values range between 0 and MAX_UINT62. -type ErrorCode = protocol.ApplicationErrorCode +type ErrorCode = qerr.ApplicationErrorCode // Err0RTTRejected is the returned from: // * Open{Uni}Stream{Sync} diff --git a/internal/mocks/quic/early_session.go b/internal/mocks/quic/early_session.go index 5d24a0b4..ef097233 100644 --- a/internal/mocks/quic/early_session.go +++ b/internal/mocks/quic/early_session.go @@ -11,7 +11,7 @@ import ( gomock "github.com/golang/mock/gomock" quic "github.com/lucas-clemente/quic-go" - protocol "github.com/lucas-clemente/quic-go/internal/protocol" + qerr "github.com/lucas-clemente/quic-go/internal/qerr" ) // MockEarlySession is a mock of EarlySession interface. @@ -68,7 +68,7 @@ func (mr *MockEarlySessionMockRecorder) AcceptUniStream(arg0 interface{}) *gomoc } // CloseWithError mocks base method. -func (m *MockEarlySession) CloseWithError(arg0 protocol.ApplicationErrorCode, arg1 string) error { +func (m *MockEarlySession) CloseWithError(arg0 qerr.ApplicationErrorCode, arg1 string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "CloseWithError", arg0, arg1) ret0, _ := ret[0].(error) diff --git a/internal/mocks/quic/stream.go b/internal/mocks/quic/stream.go index a8d899a7..992baa0d 100644 --- a/internal/mocks/quic/stream.go +++ b/internal/mocks/quic/stream.go @@ -11,6 +11,7 @@ import ( gomock "github.com/golang/mock/gomock" protocol "github.com/lucas-clemente/quic-go/internal/protocol" + qerr "github.com/lucas-clemente/quic-go/internal/qerr" ) // MockStream is a mock of Stream interface. @@ -37,7 +38,7 @@ func (m *MockStream) EXPECT() *MockStreamMockRecorder { } // CancelRead mocks base method. -func (m *MockStream) CancelRead(arg0 protocol.ApplicationErrorCode) { +func (m *MockStream) CancelRead(arg0 qerr.ApplicationErrorCode) { m.ctrl.T.Helper() m.ctrl.Call(m, "CancelRead", arg0) } @@ -49,7 +50,7 @@ func (mr *MockStreamMockRecorder) CancelRead(arg0 interface{}) *gomock.Call { } // CancelWrite mocks base method. -func (m *MockStream) CancelWrite(arg0 protocol.ApplicationErrorCode) { +func (m *MockStream) CancelWrite(arg0 qerr.ApplicationErrorCode) { m.ctrl.T.Helper() m.ctrl.Call(m, "CancelWrite", arg0) } diff --git a/internal/protocol/protocol.go b/internal/protocol/protocol.go index 1aa5f9f2..8241e274 100644 --- a/internal/protocol/protocol.go +++ b/internal/protocol/protocol.go @@ -52,9 +52,6 @@ const MaxByteCount = ByteCount(1<<62 - 1) // InvalidByteCount is an invalid byte count const InvalidByteCount ByteCount = -1 -// An ApplicationErrorCode is an application-defined error code. -type ApplicationErrorCode uint64 - // A StatelessResetToken is a stateless reset token. type StatelessResetToken [16]byte diff --git a/internal/qerr/quic_error.go b/internal/qerr/quic_error.go index 979e6a19..dadd45c9 100644 --- a/internal/qerr/quic_error.go +++ b/internal/qerr/quic_error.go @@ -46,9 +46,12 @@ func (e *TransportError) Error() string { return str + ": " + msg } +// An ApplicationErrorCode is an application-defined error code. +type ApplicationErrorCode uint64 + type ApplicationError struct { Remote bool - ErrorCode uint64 + ErrorCode ApplicationErrorCode ErrorMessage string } diff --git a/internal/wire/reset_stream_frame.go b/internal/wire/reset_stream_frame.go index 3c675591..8c2e66d8 100644 --- a/internal/wire/reset_stream_frame.go +++ b/internal/wire/reset_stream_frame.go @@ -4,13 +4,14 @@ import ( "bytes" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/quicvarint" ) // A ResetStreamFrame is a RESET_STREAM frame in QUIC type ResetStreamFrame struct { StreamID protocol.StreamID - ErrorCode protocol.ApplicationErrorCode + ErrorCode qerr.ApplicationErrorCode FinalSize protocol.ByteCount } @@ -38,7 +39,7 @@ func parseResetStreamFrame(r *bytes.Reader, _ protocol.VersionNumber) (*ResetStr return &ResetStreamFrame{ StreamID: streamID, - ErrorCode: protocol.ApplicationErrorCode(errorCode), + ErrorCode: qerr.ApplicationErrorCode(errorCode), FinalSize: byteOffset, }, nil } diff --git a/internal/wire/reset_stream_frame_test.go b/internal/wire/reset_stream_frame_test.go index 1f9996b4..61cf6c97 100644 --- a/internal/wire/reset_stream_frame_test.go +++ b/internal/wire/reset_stream_frame_test.go @@ -4,6 +4,7 @@ import ( "bytes" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/quicvarint" . "github.com/onsi/ginkgo" @@ -22,7 +23,7 @@ var _ = Describe("RESET_STREAM frame", func() { Expect(err).ToNot(HaveOccurred()) Expect(frame.StreamID).To(Equal(protocol.StreamID(0xdeadbeef))) Expect(frame.FinalSize).To(Equal(protocol.ByteCount(0x987654321))) - Expect(frame.ErrorCode).To(Equal(protocol.ApplicationErrorCode(0x1337))) + Expect(frame.ErrorCode).To(Equal(qerr.ApplicationErrorCode(0x1337))) }) It("errors on EOFs", func() { diff --git a/internal/wire/stop_sending_frame.go b/internal/wire/stop_sending_frame.go index a1448038..283f3eae 100644 --- a/internal/wire/stop_sending_frame.go +++ b/internal/wire/stop_sending_frame.go @@ -4,13 +4,14 @@ import ( "bytes" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/quicvarint" ) // A StopSendingFrame is a STOP_SENDING frame type StopSendingFrame struct { StreamID protocol.StreamID - ErrorCode protocol.ApplicationErrorCode + ErrorCode qerr.ApplicationErrorCode } // parseStopSendingFrame parses a STOP_SENDING frame @@ -30,7 +31,7 @@ func parseStopSendingFrame(r *bytes.Reader, _ protocol.VersionNumber) (*StopSend return &StopSendingFrame{ StreamID: protocol.StreamID(streamID), - ErrorCode: protocol.ApplicationErrorCode(errorCode), + ErrorCode: qerr.ApplicationErrorCode(errorCode), }, nil } diff --git a/internal/wire/stop_sending_frame_test.go b/internal/wire/stop_sending_frame_test.go index 52a7095b..e78f0c0b 100644 --- a/internal/wire/stop_sending_frame_test.go +++ b/internal/wire/stop_sending_frame_test.go @@ -4,6 +4,7 @@ import ( "bytes" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/quicvarint" . "github.com/onsi/ginkgo" @@ -20,7 +21,7 @@ var _ = Describe("STOP_SENDING frame", func() { frame, err := parseStopSendingFrame(b, versionIETFFrames) Expect(err).ToNot(HaveOccurred()) Expect(frame.StreamID).To(Equal(protocol.StreamID(0xdecafbad))) - Expect(frame.ErrorCode).To(Equal(protocol.ApplicationErrorCode(0x1337))) + Expect(frame.ErrorCode).To(Equal(qerr.ApplicationErrorCode(0x1337))) Expect(b.Len()).To(BeZero()) }) diff --git a/packet_packer.go b/packet_packer.go index b02525db..1d037ab2 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -216,7 +216,7 @@ func (p *packetPacker) PackConnectionClose(e *qerr.TransportError) (*coalescedPa // PackApplicationClose packs a packet that closes the connection with an application error. func (p *packetPacker) PackApplicationClose(e *qerr.ApplicationError) (*coalescedPacket, error) { - return p.packConnectionClose(true, e.ErrorCode, 0, e.ErrorMessage) + return p.packConnectionClose(true, uint64(e.ErrorCode), 0, e.ErrorMessage) } func (p *packetPacker) packConnectionClose( diff --git a/qlog/types_test.go b/qlog/types_test.go index cd47d6f4..89317b27 100644 --- a/qlog/types_test.go +++ b/qlog/types_test.go @@ -119,7 +119,7 @@ var _ = Describe("Types", func() { Expect(transportError(qerr.ConnectionIDLimitError).String()).To(Equal("connection_id_limit_error")) Expect(transportError(qerr.ProtocolViolation).String()).To(Equal("protocol_violation")) Expect(transportError(qerr.InvalidToken).String()).To(Equal("invalid_token")) - Expect(transportError(qerr.ApplicationError).String()).To(Equal("application_error")) + Expect(transportError(qerr.ApplicationErrorErrorCode).String()).To(Equal("application_error")) Expect(transportError(qerr.CryptoBufferExceeded).String()).To(Equal("crypto_buffer_exceeded")) Expect(transportError(qerr.NoViablePathError).String()).To(Equal("no_viable_path")) Expect(transportError(1337).String()).To(BeEmpty()) diff --git a/receive_stream.go b/receive_stream.go index 63b141bc..7df588e5 100644 --- a/receive_stream.go +++ b/receive_stream.go @@ -8,6 +8,7 @@ import ( "github.com/lucas-clemente/quic-go/internal/flowcontrol" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/wire" ) @@ -196,7 +197,7 @@ func (s *receiveStream) dequeueNextFrame() { s.readPosInFrame = 0 } -func (s *receiveStream) CancelRead(errorCode protocol.ApplicationErrorCode) { +func (s *receiveStream) CancelRead(errorCode qerr.ApplicationErrorCode) { s.mutex.Lock() completed := s.cancelReadImpl(errorCode) s.mutex.Unlock() @@ -207,7 +208,7 @@ func (s *receiveStream) CancelRead(errorCode protocol.ApplicationErrorCode) { } } -func (s *receiveStream) cancelReadImpl(errorCode protocol.ApplicationErrorCode) bool /* completed */ { +func (s *receiveStream) cancelReadImpl(errorCode qerr.ApplicationErrorCode) bool /* completed */ { if s.finRead || s.canceledRead || s.resetRemotely { return false } diff --git a/receive_stream_test.go b/receive_stream_test.go index d32fa022..45e8a771 100644 --- a/receive_stream_test.go +++ b/receive_stream_test.go @@ -9,6 +9,7 @@ import ( "github.com/golang/mock/gomock" "github.com/lucas-clemente/quic-go/internal/mocks" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/wire" . "github.com/onsi/ginkgo" @@ -574,7 +575,7 @@ var _ = Describe("Receive Stream", func() { Expect(err).To(MatchError("stream 1337 was reset with error code 1234")) Expect(err).To(BeAssignableToTypeOf(streamCanceledError{})) Expect(err.(streamCanceledError).Canceled()).To(BeTrue()) - Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(1234))) + Expect(err.(streamCanceledError).ErrorCode()).To(Equal(qerr.ApplicationErrorCode(1234))) close(done) }() Consistently(done).ShouldNot(BeClosed()) @@ -598,7 +599,7 @@ var _ = Describe("Receive Stream", func() { Expect(err).To(MatchError("stream 1337 was reset with error code 1234")) Expect(err).To(BeAssignableToTypeOf(streamCanceledError{})) Expect(err.(streamCanceledError).Canceled()).To(BeTrue()) - Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(1234))) + Expect(err.(streamCanceledError).ErrorCode()).To(Equal(qerr.ApplicationErrorCode(1234))) }) It("errors when receiving a RESET_STREAM with an inconsistent offset", func() { diff --git a/send_stream.go b/send_stream.go index 1295e2b3..75bbceba 100644 --- a/send_stream.go +++ b/send_stream.go @@ -7,9 +7,9 @@ import ( "time" "github.com/lucas-clemente/quic-go/internal/ackhandler" - "github.com/lucas-clemente/quic-go/internal/flowcontrol" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/utils" "github.com/lucas-clemente/quic-go/internal/wire" ) @@ -407,12 +407,12 @@ func (s *sendStream) Close() error { return nil } -func (s *sendStream) CancelWrite(errorCode protocol.ApplicationErrorCode) { +func (s *sendStream) CancelWrite(errorCode qerr.ApplicationErrorCode) { s.cancelWriteImpl(errorCode, fmt.Errorf("Write on stream %d canceled with error code %d", s.streamID, errorCode)) } // must be called after locking the mutex -func (s *sendStream) cancelWriteImpl(errorCode protocol.ApplicationErrorCode, writeErr error) { +func (s *sendStream) cancelWriteImpl(errorCode qerr.ApplicationErrorCode, writeErr error) { s.mutex.Lock() if s.canceledWrite { s.mutex.Unlock() diff --git a/send_stream_test.go b/send_stream_test.go index ea2d8c89..953d1dec 100644 --- a/send_stream_test.go +++ b/send_stream_test.go @@ -12,6 +12,7 @@ import ( "github.com/lucas-clemente/quic-go/internal/ackhandler" "github.com/lucas-clemente/quic-go/internal/mocks" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/wire" . "github.com/onsi/ginkgo" @@ -865,7 +866,7 @@ var _ = Describe("Send Stream", func() { Expect(err).To(MatchError("stream 1337 was reset with error code 123")) Expect(err).To(BeAssignableToTypeOf(streamCanceledError{})) Expect(err.(streamCanceledError).Canceled()).To(BeTrue()) - Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(123))) + Expect(err.(streamCanceledError).ErrorCode()).To(Equal(qerr.ApplicationErrorCode(123))) close(done) }() waitForWrite() @@ -887,7 +888,7 @@ var _ = Describe("Send Stream", func() { Expect(err).To(MatchError("stream 1337 was reset with error code 123")) Expect(err).To(BeAssignableToTypeOf(streamCanceledError{})) Expect(err.(streamCanceledError).Canceled()).To(BeTrue()) - Expect(err.(streamCanceledError).ErrorCode()).To(Equal(protocol.ApplicationErrorCode(123))) + Expect(err.(streamCanceledError).ErrorCode()).To(Equal(qerr.ApplicationErrorCode(123))) }) }) }) diff --git a/session.go b/session.go index 0b728433..15515387 100644 --- a/session.go +++ b/session.go @@ -1284,7 +1284,7 @@ func (s *session) handleConnectionCloseFrame(frame *wire.ConnectionCloseFrame) { if frame.IsApplicationError { s.closeRemote(&qerr.ApplicationError{ Remote: true, - ErrorCode: frame.ErrorCode, + ErrorCode: qerr.ApplicationErrorCode(frame.ErrorCode), ErrorMessage: frame.ReasonPhrase, }) return @@ -1477,7 +1477,7 @@ func (s *session) shutdown() { func (s *session) CloseWithError(code ErrorCode, desc string) error { s.closeLocal(&qerr.ApplicationError{ - ErrorCode: uint64(code), + ErrorCode: code, ErrorMessage: desc, }) <-s.ctx.Done() diff --git a/stream.go b/stream.go index 3237dc57..112464e7 100644 --- a/stream.go +++ b/stream.go @@ -9,6 +9,7 @@ import ( "github.com/lucas-clemente/quic-go/internal/ackhandler" "github.com/lucas-clemente/quic-go/internal/flowcontrol" "github.com/lucas-clemente/quic-go/internal/protocol" + "github.com/lucas-clemente/quic-go/internal/qerr" "github.com/lucas-clemente/quic-go/internal/wire" ) @@ -88,11 +89,11 @@ var _ Stream = &stream{} type streamCanceledError struct { error - errorCode protocol.ApplicationErrorCode + errorCode qerr.ApplicationErrorCode } -func (streamCanceledError) Canceled() bool { return true } -func (e streamCanceledError) ErrorCode() protocol.ApplicationErrorCode { return e.errorCode } +func (streamCanceledError) Canceled() bool { return true } +func (e streamCanceledError) ErrorCode() qerr.ApplicationErrorCode { return e.errorCode } var _ StreamError = &streamCanceledError{}