From dc2a14a5f7b460b481a9c6e8124024edf5f168f3 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Tue, 17 May 2016 00:31:18 +0200 Subject: [PATCH] add error code string representation to QuicError.Error --- errorcodes/error_codes.go | 2 ++ errorcodes/errorcode_string.go | 38 ++++++++++++++++++++++++++++++++++ packet_unpacker_test.go | 2 +- protocol/quic_error.go | 4 +++- protocol/quic_error_test.go | 16 ++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 errorcodes/errorcode_string.go create mode 100644 protocol/quic_error_test.go diff --git a/errorcodes/error_codes.go b/errorcodes/error_codes.go index 0fe904d0f..707b4bdc0 100644 --- a/errorcodes/error_codes.go +++ b/errorcodes/error_codes.go @@ -6,6 +6,8 @@ package errorcodes type ErrorCode uint32 // 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 diff --git a/errorcodes/errorcode_string.go b/errorcodes/errorcode_string.go new file mode 100644 index 000000000..7914f8f61 --- /dev/null +++ b/errorcodes/errorcode_string.go @@ -0,0 +1,38 @@ +// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT + +package errorcodes + +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) + } +} diff --git a/packet_unpacker_test.go b/packet_unpacker_test.go index 3ea96d914..c9f1c2554 100644 --- a/packet_unpacker_test.go +++ b/packet_unpacker_test.go @@ -163,6 +163,6 @@ var _ = Describe("Packet unpacker", func() { It("errors on invalid type", func() { setReader([]byte{0x08}) _, err := unpacker.Unpack(hdrBin, hdr, r) - Expect(err).To(MatchError("unknown type byte 0x8")) + Expect(err).To(MatchError("InvalidFrameData: unknown type byte 0x8")) }) }) diff --git a/protocol/quic_error.go b/protocol/quic_error.go index 6e83adc02..39c5716f6 100644 --- a/protocol/quic_error.go +++ b/protocol/quic_error.go @@ -1,6 +1,8 @@ package protocol import ( + "fmt" + "github.com/lucas-clemente/quic-go/errorcodes" ) @@ -19,7 +21,7 @@ func Error(errorCode errorcodes.ErrorCode, errorMessage string) *QuicError { } func (e *QuicError) Error() string { - return e.ErrorMessage + return fmt.Sprintf("%s: %s", e.ErrorCode.String(), e.ErrorMessage) } var _ error = &QuicError{} diff --git a/protocol/quic_error_test.go b/protocol/quic_error_test.go new file mode 100644 index 000000000..6e2acf858 --- /dev/null +++ b/protocol/quic_error_test.go @@ -0,0 +1,16 @@ +package protocol_test + +import ( + "github.com/lucas-clemente/quic-go/errorcodes" + "github.com/lucas-clemente/quic-go/protocol" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Quic error", func() { + It("has a string representation", func() { + err := protocol.Error(errorcodes.InternalError, "foobar") + Expect(err.Error()).To(Equal("InternalError: foobar")) + }) +})