diff --git a/callback_create.go b/callback_create.go index 260066fe..6e32922f 100644 --- a/callback_create.go +++ b/callback_create.go @@ -6,8 +6,8 @@ import ( ) func BeforeCreate(scope *Scope) { - scope.CallMethod("BeforeSave") - scope.CallMethod("BeforeCreate") + scope.CallMethodWithErrorCheck("BeforeSave") + scope.CallMethodWithErrorCheck("BeforeCreate") } func UpdateTimeStampWhenCreate(scope *Scope) { @@ -78,8 +78,8 @@ func Create(scope *Scope) { } func AfterCreate(scope *Scope) { - scope.CallMethod("AfterCreate") - scope.CallMethod("AfterSave") + scope.CallMethodWithErrorCheck("AfterCreate") + scope.CallMethodWithErrorCheck("AfterSave") } func init() { diff --git a/callback_delete.go b/callback_delete.go index c58a21fa..e46e0221 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -3,7 +3,7 @@ package gorm import "fmt" func BeforeDelete(scope *Scope) { - scope.CallMethod("BeforeDelete") + scope.CallMethodWithErrorCheck("BeforeDelete") } func Delete(scope *Scope) { @@ -24,7 +24,7 @@ func Delete(scope *Scope) { } func AfterDelete(scope *Scope) { - scope.CallMethod("AfterDelete") + scope.CallMethodWithErrorCheck("AfterDelete") } func init() { diff --git a/callback_query.go b/callback_query.go index c3671f12..b9ca58e2 100644 --- a/callback_query.go +++ b/callback_query.go @@ -103,7 +103,7 @@ func Query(scope *Scope) { } func AfterQuery(scope *Scope) { - scope.CallMethod("AfterFind") + scope.CallMethodWithErrorCheck("AfterFind") } func init() { diff --git a/callback_update.go b/callback_update.go index e7c7375a..b85e319a 100644 --- a/callback_update.go +++ b/callback_update.go @@ -26,8 +26,8 @@ func AssignUpdateAttributes(scope *Scope) { func BeforeUpdate(scope *Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { - scope.CallMethod("BeforeSave") - scope.CallMethod("BeforeUpdate") + scope.CallMethodWithErrorCheck("BeforeSave") + scope.CallMethodWithErrorCheck("BeforeUpdate") } } @@ -67,8 +67,8 @@ func Update(scope *Scope) { func AfterUpdate(scope *Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { - scope.CallMethod("AfterUpdate") - scope.CallMethod("AfterSave") + scope.CallMethodWithErrorCheck("AfterUpdate") + scope.CallMethodWithErrorCheck("AfterSave") } } diff --git a/scope.go b/scope.go index 73abc194..181aceb5 100644 --- a/scope.go +++ b/scope.go @@ -154,9 +154,8 @@ func (scope *Scope) SetColumn(column interface{}, value interface{}) error { return errors.New("could not convert column to field") } -// CallMethod invoke method with necessary argument -func (scope *Scope) CallMethod(name string) { - if scope.Value == nil { +func (scope *Scope) CallMethod(name string, checkError bool) { + if scope.Value == nil && (!checkError || !scope.HasError()) { return } @@ -190,6 +189,10 @@ func (scope *Scope) CallMethod(name string) { } } +func (scope *Scope) CallMethodWithErrorCheck(name string) { + scope.CallMethod(name, true) +} + // AddToVars add value as sql's vars, gorm will escape them func (scope *Scope) AddToVars(value interface{}) string { scope.SqlVars = append(scope.SqlVars, value)