From e6c1d5f75b9554b49e83beea8e9f68ed29b61296 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Fri, 21 Aug 2020 18:38:14 +0200 Subject: [PATCH] Fix panic on DB error (#15310) Co-authored-by: Mattermod --- app/channel.go | 2 +- app/channel_test.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/channel.go b/app/channel.go index d331dc1824..39845739db 100644 --- a/app/channel.go +++ b/app/channel.go @@ -2644,7 +2644,7 @@ func (a *App) createInitialSidebarCategories(userId, teamId string) *model.AppEr func (a *App) GetSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError) { categories, err := a.Srv().Store.Channel().GetSidebarCategories(userId, teamId) - if len(categories.Categories) == 0 && err == nil { + if err == nil && len(categories.Categories) == 0 { // A user must always have categories, so migration must not have happened yet, and we should run it ourselves nErr := a.createInitialSidebarCategories(userId, teamId) if nErr != nil { diff --git a/app/channel_test.go b/app/channel_test.go index f448697f08..ad06e15778 100644 --- a/app/channel_test.go +++ b/app/channel_test.go @@ -1999,4 +1999,23 @@ func TestGetSidebarCategories(t *testing.T) { assert.Nil(t, err) assert.Len(t, categories.Categories, 3) }) + + t.Run("should return a store error if a db table is missing", func(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + // Temporarily renaming a table to force a DB error. + sqlSupplier := mainHelper.GetSQLSupplier() + _, err := sqlSupplier.GetMaster().Exec("ALTER TABLE SidebarCategories RENAME TO SidebarCategoriesTest") + require.Nil(t, err) + defer func() { + _, err := sqlSupplier.GetMaster().Exec("ALTER TABLE SidebarCategoriesTest RENAME TO SidebarCategories") + require.Nil(t, err) + }() + + categories, appErr := th.App.GetSidebarCategories(th.BasicUser.Id, th.BasicTeam.Id) + assert.Nil(t, categories) + assert.NotNil(t, appErr) + assert.Equal(t, "store.sql_channel.sidebar_categories.app_error", appErr.Id) + }) }