Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FatalError func to avoid double negative #425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ func (err *NoFieldInTypeError) Error() string {
return fmt.Sprintf("gorp: no fields %+v in type %s", err.MissingColNames, err.TypeName)
}

// returns true if the error is non-fatal (ie, we shouldn't immediately return)
func NonFatalError(err error) bool {
// returns true if the error is fatal (ie, we should immediately return)
func FatalError(err error) bool {
switch err.(type) {
case *NoFieldInTypeError:
return true
default:
return false
default:
return true
}
}

// returns true if the error is non-fatal (ie, we shouldn't immediately return)
func NonFatalError(err error) bool {
return !FatalError(err)
}
4 changes: 2 additions & 2 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ func TestSelectTooManyCols(t *testing.T) {
var p3 FNameOnly
err := dbmap.SelectOne(&p3, "select * from person_test where "+columnName(dbmap, Person{}, "Id")+"=:Id", params)
if err != nil {
if !gorp.NonFatalError(err) {
if gorp.FatalError(err) {
t.Error(err)
}
} else {
Expand All @@ -2246,7 +2246,7 @@ func TestSelectTooManyCols(t *testing.T) {
var pSlice []FNameOnly
_, err = dbmap.Select(&pSlice, "select * from person_test order by "+columnName(dbmap, Person{}, "FName")+" asc")
if err != nil {
if !gorp.NonFatalError(err) {
if gorp.FatalError(err) {
t.Error(err)
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func SelectOne(m *DbMap, e SqlExecutor, holder interface{}, query string, args .

list, err := hookedselect(m, e, holder, query, args...)
if err != nil {
if !NonFatalError(err) { // FIXME: double negative, rename NonFatalError to FatalError
if FatalError(err) {
return err
}
nonFatalErr = err
Expand Down Expand Up @@ -178,7 +178,7 @@ func hookedselect(m *DbMap, exec SqlExecutor, i interface{}, query string,

list, err := rawselect(m, exec, i, query, args...)
if err != nil {
if !NonFatalError(err) {
if FatalError(err) {
return nil, err
}
nonFatalErr = err
Expand Down Expand Up @@ -271,7 +271,7 @@ func rawselect(m *DbMap, exec SqlExecutor, i interface{}, query string,
if intoStruct {
colToFieldIndex, err = columnToFieldIndex(m, t, tableName, cols)
if err != nil {
if !NonFatalError(err) {
if FatalError(err) {
return nil, err
}
nonFatalErr = err
Expand Down