diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-09-06 14:00:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-06 14:00:56 -0400 |
commit | 66a4d0112587608533aa744578d2db18bd88db56 (patch) | |
tree | cf1b5410162e156d5336a5c1666e7a66a405d083 /store | |
parent | f968c56890bd84295672ee0d46cc846cac2dbd47 (diff) | |
download | chat-66a4d0112587608533aa744578d2db18bd88db56.tar.gz chat-66a4d0112587608533aa744578d2db18bd88db56.tar.bz2 chat-66a4d0112587608533aa744578d2db18bd88db56.zip |
Update IsUniqueConstraint to check error codes instead of message text (#7385)
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 6 | ||||
-rw-r--r-- | store/sql_preference_store.go | 2 | ||||
-rw-r--r-- | store/sql_supplier.go | 16 | ||||
-rw-r--r-- | store/sql_supplier_reactions.go | 2 | ||||
-rw-r--r-- | store/sql_team_store.go | 4 | ||||
-rw-r--r-- | store/sql_user_store.go | 10 |
6 files changed, 25 insertions, 15 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index b18e60f81..a6261b25b 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -222,7 +222,7 @@ func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *mo } if err := transaction.Insert(channel); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Name", "channels_name_teamid_key"}) { + if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) { dupChannel := model.Channel{} s.GetMaster().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name = :Name", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name}) if dupChannel.DeleteAt > 0 { @@ -257,7 +257,7 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel { } if count, err := s.GetMaster().Update(channel); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Name", "channels_name_teamid_key"}) { + if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) { dupChannel := model.Channel{} s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name= :Name AND DeleteAt > 0", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name}) if dupChannel.DeleteAt > 0 { @@ -888,7 +888,7 @@ func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *mode } if err := transaction.Insert(member); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"ChannelId", "channelmembers_pkey"}) { + if IsUniqueConstraintError(err, []string{"ChannelId", "channelmembers_pkey"}) { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.exists.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusBadRequest) } else { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.save.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusInternalServerError) diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index e008e05eb..c2b9e3da8 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -154,7 +154,7 @@ func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *mo result := StoreResult{} if err := transaction.Insert(preference); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"UserId", "preferences_pkey"}) { + if IsUniqueConstraintError(err, []string{"UserId", "preferences_pkey"}) { result.Err = model.NewLocAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.exists.app_error", nil, "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } else { diff --git a/store/sql_supplier.go b/store/sql_supplier.go index 53153d911..2a91dbddf 100644 --- a/store/sql_supplier.go +++ b/store/sql_supplier.go @@ -16,6 +16,8 @@ import ( "time" l4g "github.com/alecthomas/log4go" + "github.com/go-sql-driver/mysql" + "github.com/lib/pq" "github.com/mattermost/gorp" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" @@ -661,11 +663,19 @@ func (ss *SqlSupplier) RemoveIndexIfExists(indexName string, tableName string) b return true } -func IsUniqueConstraintError(err string, indexName []string) bool { - unique := strings.Contains(err, "unique constraint") || strings.Contains(err, "Duplicate entry") +func IsUniqueConstraintError(err error, indexName []string) bool { + unique := false + if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" { + unique = true + } + + if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == 1062 { + unique = true + } + field := false for _, contain := range indexName { - if strings.Contains(err, contain) { + if strings.Contains(err.Error(), contain) { field = true break } diff --git a/store/sql_supplier_reactions.go b/store/sql_supplier_reactions.go index 30ca6beed..52697204c 100644 --- a/store/sql_supplier_reactions.go +++ b/store/sql_supplier_reactions.go @@ -38,7 +38,7 @@ func (s *SqlSupplier) ReactionSave(ctx context.Context, reaction *model.Reaction transaction.Rollback() // We don't consider duplicated save calls as an error - if !IsUniqueConstraintError(err.Error(), []string{"reactions_pkey", "PRIMARY"}) { + if !IsUniqueConstraintError(err, []string{"reactions_pkey", "PRIMARY"}) { result.Err = model.NewLocAppError("SqlPreferenceStore.Save", "store.sql_reaction.save.save.app_error", nil, err.Error()) } } else { diff --git a/store/sql_team_store.go b/store/sql_team_store.go index d08242bc4..747bc0568 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -79,7 +79,7 @@ func (s SqlTeamStore) Save(team *model.Team) StoreChannel { } if err := s.GetMaster().Insert(team); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Name", "teams_name_key"}) { + if IsUniqueConstraintError(err, []string{"Name", "teams_name_key"}) { result.Err = model.NewAppError("SqlTeamStore.Save", "store.sql_team.save.domain_exists.app_error", nil, "id="+team.Id+", "+err.Error(), http.StatusBadRequest) } else { result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.app_error", nil, "id="+team.Id+", "+err.Error()) @@ -514,7 +514,7 @@ func (s SqlTeamStore) SaveMember(member *model.TeamMember) StoreChannel { } if err := s.GetMaster().Insert(member); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"TeamId", "teammembers_pkey", "PRIMARY"}) { + if IsUniqueConstraintError(err, []string{"TeamId", "teammembers_pkey", "PRIMARY"}) { result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", TEAM_MEMBER_EXISTS_ERROR, nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error()) } else { result.Err = model.NewLocAppError("SqlTeamStore.SaveMember", "store.sql_team.save_member.save.app_error", nil, "team_id="+member.TeamId+", user_id="+member.UserId+", "+err.Error()) diff --git a/store/sql_user_store.go b/store/sql_user_store.go index f37a4765e..7796b7cc2 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -107,9 +107,9 @@ func (us SqlUserStore) Save(user *model.User) StoreChannel { } if err := us.GetMaster().Insert(user); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique"}) { + if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique"}) { result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.email_exists.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest) - } else if IsUniqueConstraintError(err.Error(), []string{"Username", "users_username_key", "idx_users_username_unique"}) { + } else if IsUniqueConstraintError(err, []string{"Username", "users_username_key", "idx_users_username_unique"}) { result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.username_exists.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest) } else { result.Err = model.NewLocAppError("SqlUserStore.Save", "store.sql_user.save.app_error", nil, "user_id="+user.Id+", "+err.Error()) @@ -182,9 +182,9 @@ func (us SqlUserStore) Update(user *model.User, trustedUpdateData bool) StoreCha } if count, err := us.GetMaster().Update(user); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique"}) { + if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique"}) { result.Err = model.NewAppError("SqlUserStore.Update", "store.sql_user.update.email_taken.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest) - } else if IsUniqueConstraintError(err.Error(), []string{"Username", "users_username_key", "idx_users_username_unique"}) { + } else if IsUniqueConstraintError(err, []string{"Username", "users_username_key", "idx_users_username_unique"}) { result.Err = model.NewAppError("SqlUserStore.Update", "store.sql_user.update.username_taken.app_error", nil, "user_id="+user.Id+", "+err.Error(), http.StatusBadRequest) } else { result.Err = model.NewLocAppError("SqlUserStore.Update", "store.sql_user.update.updating.app_error", nil, "user_id="+user.Id+", "+err.Error()) @@ -321,7 +321,7 @@ func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *s query += " WHERE Id = :UserId" if _, err := us.GetMaster().Exec(query, map[string]interface{}{"LastPasswordUpdate": updateAt, "UpdateAt": updateAt, "UserId": userId, "AuthService": service, "AuthData": authData, "Email": email}); err != nil { - if IsUniqueConstraintError(err.Error(), []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) { + if IsUniqueConstraintError(err, []string{"Email", "users_email_key", "idx_users_email_unique", "AuthData", "users_authdata_key"}) { result.Err = model.NewLocAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.email_exists.app_error", map[string]interface{}{"Service": service, "Email": email}, "user_id="+userId+", "+err.Error()) } else { result.Err = model.NewLocAppError("SqlUserStore.UpdateAuthData", "store.sql_user.update_auth_data.app_error", nil, "id="+userId+", "+err.Error()) |