iter: ErrNotFound bug fix

This commit is contained in:
Michał Matczuk
2017-09-22 15:02:12 +02:00
parent eef2c77a26
commit f2af2384ed
2 changed files with 31 additions and 5 deletions

View File

@@ -288,6 +288,16 @@ func TestNotFound(t *testing.T) {
Testtext string
}
t.Run("get cql error", func(t *testing.T) {
var v NotFoundTable
i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table WRONG`))
err := i.Get(&v)
if err == nil || !strings.Contains(err.Error(), "WRONG") {
t.Fatal(err)
}
})
t.Run("get", func(t *testing.T) {
var v NotFoundTable
i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table`))
@@ -296,6 +306,16 @@ func TestNotFound(t *testing.T) {
}
})
t.Run("select cql error", func(t *testing.T) {
var v []NotFoundTable
i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table WRONG`))
err := i.Select(&v)
if err == nil || !strings.Contains(err.Error(), "WRONG") {
t.Fatal(err)
}
})
t.Run("select", func(t *testing.T) {
var v []NotFoundTable
i := gocqlx.Iter(session.Query(`SELECT * FROM not_found_table`))

View File

@@ -81,7 +81,7 @@ func (iter *Iterx) scanAny(dest interface{}, structOnly bool) bool {
return false
}
if iter.Iter.NumRows() == 0 {
iter.err = gocql.ErrNotFound
// no results or query error
return false
}
@@ -135,7 +135,7 @@ func (iter *Iterx) scanAll(dest interface{}, structOnly bool) bool {
return false
}
if iter.Iter.NumRows() == 0 {
iter.err = gocql.ErrNotFound
// no results or query error
return false
}
@@ -220,7 +220,7 @@ func (iter *Iterx) StructScan(dest interface{}) bool {
}
if iter.Iter.NumRows() == 0 {
iter.err = gocql.ErrNotFound
// no results or query error
return false
}
@@ -261,9 +261,15 @@ func columnNames(ci []gocql.ColumnInfo) []string {
// the query or the iteration.
func (iter *Iterx) Close() error {
err := iter.Iter.Close()
if err != nil && iter.err == nil {
if iter.err == nil {
if err != nil {
iter.err = err
} else if iter.Iter.NumRows() == 0 {
iter.err = gocql.ErrNotFound
}
}
return iter.err
}