Skip to content

Commit

Permalink
GOCBC-635: Add InvalidIndexError to LookupInResult
Browse files Browse the repository at this point in the history
Change-Id: Ia88851e623a5054fb78a76ac4e063a3ae8dd2f24
Reviewed-on: http://review.couchbase.org/115693
Reviewed-by: Brett Lawson <brett19@gmail.com>
Tested-by: Charles Dixon <chvckd@gmail.com>
  • Loading branch information
chvck committed Oct 1, 2019
1 parent 13356f4 commit 94174aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
27 changes: 27 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ func (e serviceNotAvailableError) Error() string {
return e.message
}

// InvalidIndexError occurs when an invalid index is specified on a LookupInResult.
type InvalidIndexError interface {
InvalidIndex() bool
}

type invalidIndexError struct {
}

func (err invalidIndexError) InvalidIndex() bool {
return true
}

func (err invalidIndexError) Error() string {
return "an invalid index was specified"
}

// ServiceNotAvailableError returns whether or not the error is a service not available error.
func (e serviceNotAvailableError) ServiceNotAvailableError() bool {
return true
Expand Down Expand Up @@ -500,6 +516,17 @@ func IsSubdocPathExistsError(err error) bool {
return false
}

// IsInvalidIndexError verifies whether or not the cause for an error is due to an invalid index being specified on
// a LookupInResult
func IsInvalidIndexError(err error) bool {
switch errType := errors.Cause(err).(type) {
case InvalidIndexError:
return errType.InvalidIndex()
default:
return false
}
}

// Durability Specific Errors

// IsDurabilityError verifies whether or not the cause for an error is due to a durability error.
Expand Down
7 changes: 5 additions & 2 deletions results.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,17 @@ func (pr *lookupInPartial) exists() bool {
// ContentAt retrieves the value of the operation by its index. The index is the position of
// the operation as it was added to the builder.
func (lir *LookupInResult) ContentAt(idx int, valuePtr interface{}) error {
if idx > len(lir.contents) {
return invalidArgumentsError{message: "the supplied index was invalid"}
if idx >= len(lir.contents) {
return invalidIndexError{}
}
return lir.contents[idx].as(valuePtr, lir.serializer)
}

// Exists verifies that the item at idx exists.
func (lir *LookupInResult) Exists(idx int) bool {
if idx >= len(lir.contents) {
return false
}
return lir.contents[idx].exists()
}

Expand Down
8 changes: 2 additions & 6 deletions results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gocb

import (
"encoding/json"
"errors"
"testing"

gocbcore "github.com/couchbase/gocbcore/v8"
Expand Down Expand Up @@ -197,9 +196,6 @@ func TestLookupInResultContentAt(t *testing.T) {
{
data: contents3,
},
{
err: errors.New("error"),
},
},
serializer: &DefaultJSONSerializer{},
}
Expand Down Expand Up @@ -248,8 +244,8 @@ func TestLookupInResultContentAt(t *testing.T) {

var shouldFail string
err = res.ContentAt(3, &shouldFail)
if err == nil {
t.Fatalf("ContentAt should have failed")
if !IsInvalidIndexError(err) {
t.Fatalf("ContentAt should have failed with InvalidIndexError, was %v", err)
}

if res.Exists(3) {
Expand Down

0 comments on commit 94174aa

Please sign in to comment.