Skip to content

Commit

Permalink
Merge pull request #39 from cesarsalgado/master
Browse files Browse the repository at this point in the history
Added public GetError method to the packet.
  • Loading branch information
dareid authored Dec 1, 2017
2 parents b5101fd + b5dbf69 commit b3077dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b3077dc

Please sign in to comment.