From dd30fbc985ea4e5928184160c8dde47e19c6e61f Mon Sep 17 00:00:00 2001 From: Cesar Salgado Date: Fri, 1 Dec 2017 16:26:32 -0200 Subject: [PATCH 1/2] Added public GetError method to the packet. Before the only way for the user to get the error was to call GetResultObject. However, the parse would also be called in this case and then the user wouldn't know (programatically) where the error came from. Off course, the librarie's user could make some string matching to try to deduce, but this is error prone and non scalable. --- packet.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packet.go b/packet.go index 9f6b742..a42d713 100644 --- a/packet.go +++ b/packet.go @@ -19,6 +19,10 @@ type Packet interface { // IsError returns a boolean defining whether the Packet contains an error. IsError() bool + + // Returns the packet's error or nil if there is none. + GetError() error + // IsOk returns a boolean defining whether Packet was success IsOk() bool } @@ -54,6 +58,14 @@ func (p *packet) getStatusCode() int { return p.cmd >> 24 & 127 } +func (p *packet) GetError() error { + if p.IsError() { + return p.getError() + } else { + return nil + } +} + func (p *packet) getError() error { errDescMap := map[int]string{ 2: "Invalid expression", From b5dbf69d93dcc22ad4595d841a4dd1675f495f48 Mon Sep 17 00:00:00 2001 From: Cesar Salgado Date: Fri, 1 Dec 2017 18:34:37 -0200 Subject: [PATCH 2/2] Added two tests to the new packet.GetError method. --- packet_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packet_test.go b/packet_test.go index cbb9836..c9170ab 100644 --- a/packet_test.go +++ b/packet_test.go @@ -14,6 +14,22 @@ func TestErrorPacketIsError(t *testing.T) { } } +func TestErrorPacketGetErrorNonNil(t *testing.T) { + pkt := newErrorPacket(errors.New("test error")) + err := pkt.GetError() + if err == nil { + t.Error("GetError should return a non nil error when the packet has an error") + } +} + +func TestErrorPacketGetErrorNil(t *testing.T) { + pkt := newErrorPacket(nil) + err := pkt.GetError() + if err != nil { + t.Error("GetError should return nil when the packet has no error") + } +} + func TestErrorPacketResultObject(t *testing.T) { testError := errors.New("test error") pkt := newErrorPacket(testError)