Pass through no-rows errors to the user for .One()
This commit is contained in:
parent
1ac3853d3c
commit
956157d057
3 changed files with 11 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
package boil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
@ -101,7 +102,9 @@ func bind(q *Query, obj interface{}, structType, sliceType reflect.Type, singula
|
|||
ptrSlice = reflect.Indirect(reflect.ValueOf(obj))
|
||||
}
|
||||
|
||||
foundOne := false
|
||||
for rows.Next() {
|
||||
foundOne = true
|
||||
var newStruct reflect.Value
|
||||
var pointers []interface{}
|
||||
|
||||
|
@ -124,6 +127,10 @@ func bind(q *Query, obj interface{}, structType, sliceType reflect.Type, singula
|
|||
}
|
||||
}
|
||||
|
||||
if singular && !foundOne {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -145,6 +145,7 @@ var defaultTemplateImports = imports{
|
|||
standard: importList{
|
||||
`"fmt"`,
|
||||
`"strings"`,
|
||||
`"database/sql"`,
|
||||
},
|
||||
thirdParty: importList{
|
||||
`"github.com/pkg/errors"`,
|
||||
|
|
|
@ -18,6 +18,9 @@ func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
|||
|
||||
err := q.Bind(o)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
return nil, errors.Wrap(err, "{{.PkgName}}: failed to execute a one query for {{.Table.Name}}")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue