From 50826742fd0bd26caf55a7a5a96b2c85b612f4ae Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 18 Aug 2020 18:00:36 +0800 Subject: [PATCH] Add error gorm.ErrInvalidData --- callbacks/create.go | 2 ++ callbacks/update.go | 2 ++ errors.go | 2 ++ tests/update_test.go | 9 +++++++++ 4 files changed, 15 insertions(+) diff --git a/callbacks/create.go b/callbacks/create.go index 4cc0f555..7a32ed5c 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -309,6 +309,8 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { } } } + default: + stmt.AddError(gorm.ErrInvalidData) } } diff --git a/callbacks/update.go b/callbacks/update.go index 0ced3ffb..5656d166 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -252,6 +252,8 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { } } } + default: + stmt.AddError(gorm.ErrInvalidData) } } diff --git a/errors.go b/errors.go index 115b8e25..32ff8ec1 100644 --- a/errors.go +++ b/errors.go @@ -19,6 +19,8 @@ var ( ErrPrimaryKeyRequired = errors.New("primary key required") // ErrModelValueRequired model value required ErrModelValueRequired = errors.New("model value required") + // ErrInvalidData unsupported data + ErrInvalidData = errors.New("unsupported data") // ErrUnsupportedDriver unsupported driver ErrUnsupportedDriver = errors.New("unsupported driver") // ErrRegistered registered diff --git a/tests/update_test.go b/tests/update_test.go index a59a8856..49a13be9 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -334,6 +334,15 @@ func TestSelectWithUpdateWithMap(t *testing.T) { AssertObjEqual(t, result2, result, "Name", "Account", "Toys", "Manager", "ManagerID", "Languages") } +func TestWithUpdateWithInvalidMap(t *testing.T) { + user := *GetUser("update_with_invalid_map", Config{}) + DB.Create(&user) + + if err := DB.Model(&user).Updates(map[string]string{"name": "jinzhu"}).Error; !errors.Is(err, gorm.ErrInvalidData) { + t.Errorf("should returns error for unsupported updating data") + } +} + func TestOmitWithUpdate(t *testing.T) { user := *GetUser("omit_update", Config{Account: true, Pets: 3, Toys: 3, Company: true, Manager: true, Team: 3, Languages: 3, Friends: 4}) DB.Create(&user)