feat: support go1.13 errors.Is

This commit is contained in:
chyroc 2019-10-20 20:22:33 +08:00
parent 5b3e40ac12
commit 937ae81751
No known key found for this signature in database
GPG Key ID: 5CEACB2EE6A2FEFA
4 changed files with 18 additions and 4 deletions

7
error_12.go Normal file
View File

@ -0,0 +1,7 @@
// +build !go1.13
package gorm
func isError(err, target error) bool {
return err == target
}

7
error_13.go Normal file
View File

@ -0,0 +1,7 @@
// +build go1.13
package gorm
import "errors"
var isError = errors.Is

View File

@ -25,12 +25,12 @@ type Errors []error
func IsRecordNotFoundError(err error) bool { func IsRecordNotFoundError(err error) bool {
if errs, ok := err.(Errors); ok { if errs, ok := err.(Errors); ok {
for _, err := range errs { for _, err := range errs {
if err == ErrRecordNotFound { if isError(err, ErrRecordNotFound) {
return true return true
} }
} }
} }
return err == ErrRecordNotFound return isError(err, ErrRecordNotFound)
} }
// GetErrors gets all errors that have occurred and returns a slice of errors (Error type) // GetErrors gets all errors that have occurred and returns a slice of errors (Error type)

View File

@ -592,7 +592,7 @@ func (s *DB) NewRecord(value interface{}) bool {
// RecordNotFound check if returning ErrRecordNotFound error // RecordNotFound check if returning ErrRecordNotFound error
func (s *DB) RecordNotFound() bool { func (s *DB) RecordNotFound() bool {
for _, err := range s.GetErrors() { for _, err := range s.GetErrors() {
if err == ErrRecordNotFound { if isError(err, ErrRecordNotFound) {
return true return true
} }
} }
@ -776,7 +776,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
// AddError add error to the db // AddError add error to the db
func (s *DB) AddError(err error) error { func (s *DB) AddError(err error) error {
if err != nil { if err != nil {
if err != ErrRecordNotFound { if !isError(err, ErrRecordNotFound) {
if s.logMode == defaultLogMode { if s.logMode == defaultLogMode {
go s.print("error", fileWithLineNum(), err) go s.print("error", fileWithLineNum(), err)
} else { } else {