From 956157d05756c9cd429e55b60c925ef15ed1b32a Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 13 Aug 2016 11:42:28 -0700 Subject: [PATCH] Pass through no-rows errors to the user for .One() --- boil/reflect.go | 7 +++++++ imports.go | 1 + templates/03_finishers.tpl | 3 +++ 3 files changed, 11 insertions(+) diff --git a/boil/reflect.go b/boil/reflect.go index f4af760..1fdb44a 100644 --- a/boil/reflect.go +++ b/boil/reflect.go @@ -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 } diff --git a/imports.go b/imports.go index 5b5f094..04cd217 100644 --- a/imports.go +++ b/imports.go @@ -145,6 +145,7 @@ var defaultTemplateImports = imports{ standard: importList{ `"fmt"`, `"strings"`, + `"database/sql"`, }, thirdParty: importList{ `"github.com/pkg/errors"`, diff --git a/templates/03_finishers.tpl b/templates/03_finishers.tpl index 276ba31..be887dd 100644 --- a/templates/03_finishers.tpl +++ b/templates/03_finishers.tpl @@ -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}}") }