Skip to content

Commit

Permalink
Don't call callbacks if has error
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 24, 2015
1 parent 5586d04 commit 83ee11e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gorm
import "fmt"

func BeforeDelete(scope *Scope) {
scope.CallMethod("BeforeDelete")
scope.CallMethodWithErrorCheck("BeforeDelete")
}

func Delete(scope *Scope) {
Expand All @@ -24,7 +24,7 @@ func Delete(scope *Scope) {
}

func AfterDelete(scope *Scope) {
scope.CallMethod("AfterDelete")
scope.CallMethodWithErrorCheck("AfterDelete")
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Query(scope *Scope) {
}

func AfterQuery(scope *Scope) {
scope.CallMethod("AfterFind")
scope.CallMethodWithErrorCheck("AfterFind")
}

func init() {
Expand Down
8 changes: 4 additions & 4 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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")
}
}

Expand Down
9 changes: 6 additions & 3 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 83ee11e

Please sign in to comment.