SqlTermsOfService migrates to plain errors (#14675)

* Advances migragint TermsOfService

* Advances...

* Replaced message key

* Replaced message key

* Replaced message key

* Replaced message key

* Refactor name of errors

* Fix mixed type of error

* Fix-imports

* i18n-extract

* Rollback changes

* Rollback changes

* Rollback changes

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Rodrigo Villablanca
2020-06-02 13:17:52 -04:00
коммит произвёл GitHub
родитель 85a69d6112
Коммит 52cf817c8e
11 изменённых файлов: 104 добавлений и 71 удалений

Просмотреть файл

@@ -33,7 +33,7 @@ func (s LocalCacheTermsOfServiceStore) ClearCaches() {
}
}
func (s LocalCacheTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
func (s LocalCacheTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
tos, err := s.TermsOfServiceStore.Save(termsOfService)
if err == nil {
@@ -43,7 +43,7 @@ func (s LocalCacheTermsOfServiceStore) Save(termsOfService *model.TermsOfService
return tos, err
}
func (s LocalCacheTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s LocalCacheTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
if allowFromCache {
if s.rootStore.termsOfServiceCache.Len() != 0 {
if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, LATEST_KEY); cacheItem != nil {
@@ -62,7 +62,7 @@ func (s LocalCacheTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.Te
return termsOfService, err
}
func (s LocalCacheTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s LocalCacheTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
if allowFromCache {
if cacheItem := s.rootStore.doStandardReadCache(s.rootStore.termsOfServiceCache, id); cacheItem != nil {
return cacheItem.(*model.TermsOfService), nil

Просмотреть файл

@@ -7095,7 +7095,7 @@ func (s *OpenTracingLayerTeamStore) UserBelongsToTeams(userId string, teamIds []
return resultVar0, resultVar1
}
func (s *OpenTracingLayerTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s *OpenTracingLayerTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "TermsOfServiceStore.Get")
s.Root.Store.SetContext(newCtx)
@@ -7113,7 +7113,7 @@ func (s *OpenTracingLayerTermsOfServiceStore) Get(id string, allowFromCache bool
return resultVar0, resultVar1
}
func (s *OpenTracingLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s *OpenTracingLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "TermsOfServiceStore.GetLatest")
s.Root.Store.SetContext(newCtx)
@@ -7131,7 +7131,7 @@ func (s *OpenTracingLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*m
return resultVar0, resultVar1
}
func (s *OpenTracingLayerTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
func (s *OpenTracingLayerTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "TermsOfServiceStore.Save")
s.Root.Store.SetContext(newCtx)

Просмотреть файл

@@ -5,11 +5,12 @@ package sqlstore
import (
"database/sql"
"net/http"
"github.com/mattermost/mattermost-server/v5/einterfaces"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/pkg/errors"
)
type SqlTermsOfServiceStore struct {
@@ -33,9 +34,9 @@ func newSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInt
func (s SqlTermsOfServiceStore) createIndexesIfNotExists() {
}
func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
if len(termsOfService.Id) > 0 {
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service_store.save.existing.app_error", nil, "id="+termsOfService.Id, http.StatusBadRequest)
return nil, store.NewErrInvalidInput("TermsOfService", "Id", termsOfService.Id)
}
termsOfService.PreSave()
@@ -45,33 +46,33 @@ func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*mod
}
if err := s.GetMaster().Insert(termsOfService); err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service.save.app_error", nil, "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "could not save a new TermsOfService")
}
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
var termsOfService *model.TermsOfService
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
if err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
return nil, store.NewErrNotFound("TermsOfService", "CreateAt=latest")
}
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "could not find latest TermsOfService")
}
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
if err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "could not find TermsOfService with id=%s", id)
}
if obj == nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
return nil, store.NewErrNotFound("TermsOfService", id)
}
return obj.(*model.TermsOfService), nil
}

Просмотреть файл

@@ -613,9 +613,9 @@ type SchemeStore interface {
}
type TermsOfServiceStore interface {
Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError)
GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError)
Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError)
Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error)
GetLatest(allowFromCache bool) (*model.TermsOfService, error)
Get(id string, allowFromCache bool) (*model.TermsOfService, error)
}
type UserTermsOfServiceStore interface {

Просмотреть файл

@@ -15,7 +15,7 @@ type TermsOfServiceStore struct {
}
// Get provides a mock function with given fields: id, allowFromCache
func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
ret := _m.Called(id, allowFromCache)
var r0 *model.TermsOfService
@@ -27,20 +27,18 @@ func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) (*model.Terms
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, bool) *model.AppError); ok {
var r1 error
if rf, ok := ret.Get(1).(func(string, bool) error); ok {
r1 = rf(id, allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
r1 = ret.Error(1)
}
return r0, r1
}
// GetLatest provides a mock function with given fields: allowFromCache
func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
ret := _m.Called(allowFromCache)
var r0 *model.TermsOfService
@@ -52,20 +50,18 @@ func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfSer
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(bool) *model.AppError); ok {
var r1 error
if rf, ok := ret.Get(1).(func(bool) error); ok {
r1 = rf(allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
r1 = ret.Error(1)
}
return r0, r1
}
// Save provides a mock function with given fields: termsOfService
func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
ret := _m.Called(termsOfService)
var r0 *model.TermsOfService
@@ -77,13 +73,11 @@ func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*mode
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.TermsOfService) *model.AppError); ok {
var r1 error
if rf, ok := ret.Get(1).(func(*model.TermsOfService) error); ok {
r1 = rf(termsOfService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
r1 = ret.Error(1)
}
return r0, r1

Просмотреть файл

@@ -23,8 +23,8 @@ func testSaveTermsOfService(t *testing.T, ss store.Store) {
u1.Username = model.NewId()
u1.Email = MakeEmail()
u1.Nickname = model.NewId()
_, err := ss.User().Save(&u1)
require.Nil(t, err)
_, appErr := ss.User().Save(&u1)
require.Nil(t, appErr)
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
savedTermsOfService, err := ss.TermsOfService().Save(termsOfService)
@@ -40,11 +40,11 @@ func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
u1.Username = model.NewId()
u1.Email = MakeEmail()
u1.Nickname = model.NewId()
_, err := ss.User().Save(&u1)
require.Nil(t, err)
_, appErr := ss.User().Save(&u1)
require.Nil(t, appErr)
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
_, err = ss.TermsOfService().Save(termsOfService)
_, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
fetchedTermsOfService, err := ss.TermsOfService().GetLatest(true)
@@ -58,11 +58,11 @@ func testGetTermsOfService(t *testing.T, ss store.Store) {
u1.Username = model.NewId()
u1.Email = MakeEmail()
u1.Nickname = model.NewId()
_, err := ss.User().Save(&u1)
require.Nil(t, err)
_, appErr := ss.User().Save(&u1)
require.Nil(t, appErr)
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
_, err = ss.TermsOfService().Save(termsOfService)
_, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
r1, err := ss.TermsOfService().Get("an_invalid_id", true)

Просмотреть файл

@@ -6409,7 +6409,7 @@ func (s *TimerLayerTeamStore) UserBelongsToTeams(userId string, teamIds []string
return resultVar0, resultVar1
}
func (s *TimerLayerTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s *TimerLayerTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
start := timemodule.Now()
resultVar0, resultVar1 := s.TermsOfServiceStore.Get(id, allowFromCache)
@@ -6425,7 +6425,7 @@ func (s *TimerLayerTermsOfServiceStore) Get(id string, allowFromCache bool) (*mo
return resultVar0, resultVar1
}
func (s *TimerLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
func (s *TimerLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
start := timemodule.Now()
resultVar0, resultVar1 := s.TermsOfServiceStore.GetLatest(allowFromCache)
@@ -6441,7 +6441,7 @@ func (s *TimerLayerTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.T
return resultVar0, resultVar1
}
func (s *TimerLayerTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
func (s *TimerLayerTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
start := timemodule.Now()
resultVar0, resultVar1 := s.TermsOfServiceStore.Save(termsOfService)