diff --git a/app/channel.go b/app/channel.go index 8261738c75..337743b822 100644 --- a/app/channel.go +++ b/app/channel.go @@ -1563,7 +1563,12 @@ func (a *App) GetAllChannels(page, perPage int, opts model.ChannelSearchOpts) (* NotAssociatedToGroup: opts.NotAssociatedToGroup, IncludeDeleted: opts.IncludeDeleted, } - return a.Srv().Store.Channel().GetAllChannels(page*perPage, perPage, storeOpts) + channels, err := a.Srv().Store.Channel().GetAllChannels(page*perPage, perPage, storeOpts) + if err != nil { + return nil, model.NewAppError("GetAllChannels", "app.channel.get_all_channels.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return channels, nil } func (a *App) GetAllChannelsCount(opts model.ChannelSearchOpts) (int64, *model.AppError) { @@ -1575,7 +1580,12 @@ func (a *App) GetAllChannelsCount(opts model.ChannelSearchOpts) (int64, *model.A NotAssociatedToGroup: opts.NotAssociatedToGroup, IncludeDeleted: opts.IncludeDeleted, } - return a.Srv().Store.Channel().GetAllChannelsCount(storeOpts) + count, err := a.Srv().Store.Channel().GetAllChannelsCount(storeOpts) + if err != nil { + return 0, model.NewAppError("GetAllChannelsCount", "app.channel.get_all_channels_count.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return count, nil } func (a *App) GetDeletedChannels(teamId string, offset int, limit int, userId string) (*model.ChannelList, *model.AppError) { diff --git a/i18n/en.json b/i18n/en.json index 0cf6a7f324..eee42bf7a2 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -2998,6 +2998,14 @@ "id": "app.channel.get.find.app_error", "translation": "We encountered an error finding the channel." }, + { + "id": "app.channel.get_all_channels.app_error", + "translation": "Unable to get all the channels." + }, + { + "id": "app.channel.get_all_channels_count.app_error", + "translation": "Unable to count all the channels." + }, { "id": "app.channel.move_channel.members_do_not_match.error", "translation": "Unable to move a channel unless all its members are already members of the destination team." @@ -6126,10 +6134,6 @@ "id": "store.sql_channel.get_all.app_error", "translation": "Unable to get all the channels." }, - { - "id": "store.sql_channel.get_all_channels.get.app_error", - "translation": "Unable to get all the channels." - }, { "id": "store.sql_channel.get_all_direct.app_error", "translation": "Unable to get all the direct channels." diff --git a/store/opentracing_layer.go b/store/opentracing_layer.go index 8d8427218f..1da2074bd1 100644 --- a/store/opentracing_layer.go +++ b/store/opentracing_layer.go @@ -702,7 +702,7 @@ func (s *OpenTracingLayerChannelStore) GetAllChannelMembersNotifyPropsForChannel return resultVar0, resultVar1 } -func (s *OpenTracingLayerChannelStore) GetAllChannels(page int, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) { +func (s *OpenTracingLayerChannelStore) GetAllChannels(page int, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.GetAllChannels") s.Root.Store.SetContext(newCtx) @@ -720,7 +720,7 @@ func (s *OpenTracingLayerChannelStore) GetAllChannels(page int, perPage int, opt return resultVar0, resultVar1 } -func (s *OpenTracingLayerChannelStore) GetAllChannelsCount(opts ChannelSearchOpts) (int64, *model.AppError) { +func (s *OpenTracingLayerChannelStore) GetAllChannelsCount(opts ChannelSearchOpts) (int64, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.GetAllChannelsCount") s.Root.Store.SetContext(newCtx) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index 34b6ab5fb0..cef687c518 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -931,37 +931,37 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDelete return channels, nil } -func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) { +func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, error) { query := s.getAllChannelsQuery(opts, false) query = query.OrderBy("c.DisplayName, Teams.DisplayName").Limit(uint64(limit)).Offset(uint64(offset)) queryString, args, err := query.ToSql() if err != nil { - return nil, model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrap(err, "failed to create query") } data := &model.ChannelListWithTeamData{} _, err = s.GetReplica().Select(data, queryString, args...) if err != nil { - return nil, model.NewAppError("SqlChannelStore.GetAllChannels", "store.sql_channel.get_all_channels.get.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrap(err, "failed to get all channels") } return data, nil } -func (s SqlChannelStore) GetAllChannelsCount(opts store.ChannelSearchOpts) (int64, *model.AppError) { +func (s SqlChannelStore) GetAllChannelsCount(opts store.ChannelSearchOpts) (int64, error) { query := s.getAllChannelsQuery(opts, true) queryString, args, err := query.ToSql() if err != nil { - return 0, model.NewAppError("SqlChannelStore.GetAllChannelsCount", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError) + return 0, errors.Wrap(err, "failed to create query") } count, err := s.GetReplica().SelectInt(queryString, args...) if err != nil { - return 0, model.NewAppError("SqlChannelStore.GetAllChannelsCount", "store.sql_channel.get_all_channels.get.app_error", nil, err.Error(), http.StatusInternalServerError) + return 0, errors.Wrap(err, "failed to count all channels") } return count, nil diff --git a/store/store.go b/store/store.go index c29c924ee2..8b943fde17 100644 --- a/store/store.go +++ b/store/store.go @@ -149,8 +149,8 @@ type ChannelStore interface { GetDeletedByName(team_id string, name string) (*model.Channel, *model.AppError) GetDeleted(team_id string, offset int, limit int, userId string) (*model.ChannelList, *model.AppError) GetChannels(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) - GetAllChannels(page, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) - GetAllChannelsCount(opts ChannelSearchOpts) (int64, *model.AppError) + GetAllChannels(page, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, error) + GetAllChannelsCount(opts ChannelSearchOpts) (int64, error) GetMoreChannels(teamId string, userId string, offset int, limit int) (*model.ChannelList, *model.AppError) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 8f94963e5c..f360c7e809 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -3249,45 +3249,45 @@ func testChannelStoreGetAllChannels(t *testing.T, ss store.Store, s SqlSupplier) _, nErr = ss.Channel().Save(&c5, -1) require.Nil(t, nErr) - list, err := ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{}) - require.Nil(t, err) + list, nErr := ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{}) + require.Nil(t, nErr) assert.Len(t, *list, 2) assert.Equal(t, c1.Id, (*list)[0].Id) assert.Equal(t, "Name", (*list)[0].TeamDisplayName) assert.Equal(t, c3.Id, (*list)[1].Id) assert.Equal(t, "Name2", (*list)[1].TeamDisplayName) - count1, err := ss.Channel().GetAllChannelsCount(store.ChannelSearchOpts{}) - require.Nil(t, err) + count1, nErr := ss.Channel().GetAllChannelsCount(store.ChannelSearchOpts{}) + require.Nil(t, nErr) - list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{IncludeDeleted: true}) - require.Nil(t, err) + list, nErr = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{IncludeDeleted: true}) + require.Nil(t, nErr) assert.Len(t, *list, 3) assert.Equal(t, c1.Id, (*list)[0].Id) assert.Equal(t, "Name", (*list)[0].TeamDisplayName) assert.Equal(t, c2.Id, (*list)[1].Id) assert.Equal(t, c3.Id, (*list)[2].Id) - count2, err := ss.Channel().GetAllChannelsCount(store.ChannelSearchOpts{IncludeDeleted: true}) - require.Nil(t, err) + count2, nErr := ss.Channel().GetAllChannelsCount(store.ChannelSearchOpts{IncludeDeleted: true}) + require.Nil(t, nErr) require.True(t, func() bool { return count2 > count1 }()) - list, err = ss.Channel().GetAllChannels(0, 1, store.ChannelSearchOpts{IncludeDeleted: true}) - require.Nil(t, err) + list, nErr = ss.Channel().GetAllChannels(0, 1, store.ChannelSearchOpts{IncludeDeleted: true}) + require.Nil(t, nErr) assert.Len(t, *list, 1) assert.Equal(t, c1.Id, (*list)[0].Id) assert.Equal(t, "Name", (*list)[0].TeamDisplayName) // Not associated to group - list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{NotAssociatedToGroup: group.Id}) - require.Nil(t, err) + list, nErr = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{NotAssociatedToGroup: group.Id}) + require.Nil(t, nErr) assert.Len(t, *list, 1) // Exclude channel names - list, err = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{ExcludeChannelNames: []string{c1.Name}}) - require.Nil(t, err) + list, nErr = ss.Channel().GetAllChannels(0, 10, store.ChannelSearchOpts{ExcludeChannelNames: []string{c1.Name}}) + require.Nil(t, nErr) assert.Len(t, *list, 1) // Manually truncate Channels table until testlib can handle cleanups diff --git a/store/storetest/compliance_store.go b/store/storetest/compliance_store.go index a3e457b3e0..77b1a58c13 100644 --- a/store/storetest/compliance_store.go +++ b/store/storetest/compliance_store.go @@ -28,8 +28,8 @@ func cleanupStoreState(t *testing.T, ss store.Store) { } //remove existing channels - allChannels, err := ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true}) - require.Nilf(t, err, "error cleaning all test channels: %v", err) + allChannels, nErr := ss.Channel().GetAllChannels(0, 100000, store.ChannelSearchOpts{IncludeDeleted: true}) + require.Nilf(t, nErr, "error cleaning all test channels: %v", nErr) for _, channel := range *allChannels { nErr := ss.Channel().PermanentDelete(channel.Id) require.Nil(t, nErr, "failed cleaning up test channel %s", channel.Id) diff --git a/store/storetest/mocks/ChannelMemberHistoryStore.go b/store/storetest/mocks/ChannelMemberHistoryStore.go index 6a097c159b..7f23d7778d 100644 --- a/store/storetest/mocks/ChannelMemberHistoryStore.go +++ b/store/storetest/mocks/ChannelMemberHistoryStore.go @@ -31,9 +31,7 @@ func (_m *ChannelMemberHistoryStore) GetUsersInChannelDuring(startTime int64, en if rf, ok := ret.Get(1).(func(int64, int64, string) error); ok { r1 = rf(startTime, endTime, channelId) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(error) - } + r1 = ret.Error(1) } return r0, r1 @@ -47,9 +45,7 @@ func (_m *ChannelMemberHistoryStore) LogJoinEvent(userId string, channelId strin if rf, ok := ret.Get(0).(func(string, string, int64) error); ok { r0 = rf(userId, channelId, joinTime) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(error) - } + r0 = ret.Error(0) } return r0 @@ -63,9 +59,7 @@ func (_m *ChannelMemberHistoryStore) LogLeaveEvent(userId string, channelId stri if rf, ok := ret.Get(0).(func(string, string, int64) error); ok { r0 = rf(userId, channelId, leaveTime) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(error) - } + r0 = ret.Error(0) } return r0 @@ -86,9 +80,7 @@ func (_m *ChannelMemberHistoryStore) PermanentDeleteBatch(endTime int64, limit i if rf, ok := ret.Get(1).(func(int64, int64) error); ok { r1 = rf(endTime, limit) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(error) - } + r1 = ret.Error(1) } return r0, r1 diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index 740e648219..19f2cb5bc1 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -291,7 +291,7 @@ func (_m *ChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId stri } // GetAllChannels provides a mock function with given fields: page, perPage, opts -func (_m *ChannelStore) GetAllChannels(page int, perPage int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) { +func (_m *ChannelStore) GetAllChannels(page int, perPage int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, error) { ret := _m.Called(page, perPage, opts) var r0 *model.ChannelListWithTeamData @@ -303,20 +303,18 @@ func (_m *ChannelStore) GetAllChannels(page int, perPage int, opts store.Channel } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(int, int, store.ChannelSearchOpts) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(int, int, store.ChannelSearchOpts) error); ok { r1 = rf(page, perPage, opts) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // GetAllChannelsCount provides a mock function with given fields: opts -func (_m *ChannelStore) GetAllChannelsCount(opts store.ChannelSearchOpts) (int64, *model.AppError) { +func (_m *ChannelStore) GetAllChannelsCount(opts store.ChannelSearchOpts) (int64, error) { ret := _m.Called(opts) var r0 int64 @@ -326,13 +324,11 @@ func (_m *ChannelStore) GetAllChannelsCount(opts store.ChannelSearchOpts) (int64 r0 = ret.Get(0).(int64) } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(store.ChannelSearchOpts) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(store.ChannelSearchOpts) error); ok { r1 = rf(opts) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 diff --git a/store/timer_layer.go b/store/timer_layer.go index 8864cd30ce..85db3c19f1 100644 --- a/store/timer_layer.go +++ b/store/timer_layer.go @@ -664,7 +664,7 @@ func (s *TimerLayerChannelStore) GetAllChannelMembersNotifyPropsForChannel(chann return resultVar0, resultVar1 } -func (s *TimerLayerChannelStore) GetAllChannels(page int, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, *model.AppError) { +func (s *TimerLayerChannelStore) GetAllChannels(page int, perPage int, opts ChannelSearchOpts) (*model.ChannelListWithTeamData, error) { start := timemodule.Now() resultVar0, resultVar1 := s.ChannelStore.GetAllChannels(page, perPage, opts) @@ -680,7 +680,7 @@ func (s *TimerLayerChannelStore) GetAllChannels(page int, perPage int, opts Chan return resultVar0, resultVar1 } -func (s *TimerLayerChannelStore) GetAllChannelsCount(opts ChannelSearchOpts) (int64, *model.AppError) { +func (s *TimerLayerChannelStore) GetAllChannelsCount(opts ChannelSearchOpts) (int64, error) { start := timemodule.Now() resultVar0, resultVar1 := s.ChannelStore.GetAllChannelsCount(opts)