Repository: thundernest/k-9
Issue: Crash when trying to open a non-existent folder (via unread widget)

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

5.913

### Where did you get the app from?

Google Play

### Android version

doesn't matter

### Device model

_No response_

### Steps to reproduce

1. Add an "K-9 Unread" widget that references a particular folder to the home screen
2. Delete that folder from the server
3. Refresh the folder list in K-9 Mail (so the folder disappears)
4. Attempt to open the app using the "K-9 Unread" widget that references the now non-existent folder

### Expected behavior

Don't crash

### Actual behavior

Crash

### Logs

Stacktrace received via Google Play developer console:

```text
java.lang.RuntimeException: 
  at com.fsck.k9.fragment.MessageListFragment.getFolderInfoHolder (MessageListFragment.kt:465)
  at com.fsck.k9.fragment.MessageListFragment.decodeArguments (MessageListFragment.kt:203)
  at com.fsck.k9.fragment.MessageListFragment.onCreate (MessageListFragment.kt:150)
  at androidx.fragment.app.Fragment.performCreate (Fragment.java:2981)
  at androidx.fragment.app.FragmentStateManager.create (FragmentStateManager.java:474)
  at androidx.fragment.app.FragmentStateManager.moveToExpectedState (FragmentStateManager.java:257)
  at androidx.fragment.app.FragmentManager.executeOpsTogether (FragmentManager.java:1840)
  at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute (FragmentManager.java:1758)
  at androidx.fragment.app.FragmentManager.execPendingActions (FragmentManager.java:1701)
  at androidx.fragment.app.FragmentManager$4.run (FragmentManager.java:488)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:233)
  at android.app.ActivityThread.main (ActivityThread.java:8052)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:656)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:967)
Caused by: com.fsck.k9.mail.MessagingException: 
  at com.fsck.k9.mailstore.LocalFolder$1.doDbWork (LocalFolder.java:160)
  at com.fsck.k9.mailstore.LocalFolder$1.doDbWork (LocalFolder.java:142)
  at com.fsck.k9.mailstore.LockableDatabase.execute (LockableDatabase.java:169)
  at com.fsck.k9.mailstore.LocalFolder.open (LocalFolder.java:142)
  at com.fsck.k9.fragment.MlfUtils.getOpenFolder (MlfUtils.java:28)
  at com.fsck.k9.fragment.MessageListFragment.getFolderInfoHolder (MessageListFragment.kt:462)
```

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListFragment.kt, app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt
index da6bce71a..df2d18090 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageList.kt
@@ -1639,6 +1639,11 @@ open class MessageList :
         permissionUiHelper.requestPermission(permission)
     }
 
+    override fun onFolderNotFoundError() {
+        val defaultFolderId = defaultFolderProvider.getDefaultFolder(account!!)
+        openFolderImmediately(defaultFolderId)
+    }
+
     private enum class DisplayMode {
         MESSAGE_LIST, MESSAGE_VIEW, SPLIT_VIEW
     }
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListFragment.kt b/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListFragment.kt
index c1cc821f1..237a6a408 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListFragment.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListFragment.kt
@@ -147,7 +147,7 @@ class MessageListFragment :
         super.onCreate(savedInstanceState)
 
         restoreInstanceState(savedInstanceState)
-        decodeArguments()
+        decodeArguments() ?: return
 
         viewModel.getMessageListLiveData().observe(this) { messageListInfo: MessageListInfo ->
             setMessageList(messageListInfo)
@@ -177,7 +177,7 @@ class MessageListFragment :
         listView.onRestoreInstanceState(savedListState)
     }
 
-    private fun decodeArguments() {
+    private fun decodeArguments(): MessageListFragment? {
         val arguments = requireArguments()
         showingThreadedList = arguments.getBoolean(ARG_THREADED_LIST, false)
         isThreadDisplay = arguments.getBoolean(ARG_IS_THREAD_DISPLAY, false)
@@ -198,10 +198,17 @@ class MessageListFragment :
 
         isSingleFolderMode = false
         if (isSingleAccountMode && localSearch.folderIds.size == 1) {
-            isSingleFolderMode = true
-            val folderId = localSearch.folderIds[0]
-            currentFolder = getFolderInfoHolder(folderId, account!!)
+            try {
+                val folderId = localSearch.folderIds[0]
+                currentFolder = getFolderInfoHolder(folderId, account!!)
+                isSingleFolderMode = true
+            } catch (e: MessagingException) {
+                fragmentListener.onFolderNotFoundError()
+                return null
+            }
         }
+
+        return this
     }
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@@ -458,12 +465,8 @@ class MessageListFragment :
         )
 
     private fun getFolderInfoHolder(folderId: Long, account: Account): FolderInfoHolder {
-        return try {
-            val localFolder = MlfUtils.getOpenFolder(folderId, account)
-            FolderInfoHolder(folderNameFormatter, localFolder, account)
-        } catch (e: MessagingException) {
-            throw RuntimeException(e)
-        }
+        val localFolder = MlfUtils.getOpenFolder(folderId, account)
+        return FolderInfoHolder(folderNameFormatter, localFolder, account)
     }
 
     override fun onResume() {
@@ -1926,6 +1929,7 @@ class MessageListFragment :
         fun remoteSearchStarted()
         fun goBack()
         fun updateMenu()
+        fun onFolderNotFoundError()
 
         companion object {
             const val MAX_PROGRESS = 10000