From 8b70f3a3b98bb10f78ad55df7b056ca712aca32f Mon Sep 17 00:00:00 2001
From: Adam Brown <adampsbrown@gmail.com>
Date: Wed, 17 Aug 2022 16:16:44 +0100
Subject: [PATCH] extracting throwable crawling to extension

---
 .../vector/app/core/extensions/Throwable.kt   | 34 +++++++++++++++++++
 .../ftueauth/FtueAuthCaptchaFragment.kt       | 13 +------
 2 files changed, 35 insertions(+), 12 deletions(-)
 create mode 100644 vector/src/main/java/im/vector/app/core/extensions/Throwable.kt

diff --git a/vector/src/main/java/im/vector/app/core/extensions/Throwable.kt b/vector/src/main/java/im/vector/app/core/extensions/Throwable.kt
new file mode 100644
index 0000000000..e1688124fa
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/core/extensions/Throwable.kt
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022 New Vector Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package im.vector.app.core.extensions
+
+/**
+ * Recursive through the throwable and its causes for the given predicate
+ *
+ * @return true when the predicate finds a match
+ */
+tailrec fun Throwable?.crawlCausesFor(predicate: (Throwable) -> Boolean): Boolean {
+    return when {
+        this == null -> false
+        else -> {
+            when (predicate(this)) {
+                true -> true
+                else -> this.cause.crawlCausesFor(predicate)
+            }
+        }
+    }
+}
diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthCaptchaFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthCaptchaFragment.kt
index 0562378ba5..1f44922b3b 100644
--- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthCaptchaFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthCaptchaFragment.kt
@@ -25,6 +25,7 @@ import android.view.ViewStub
 import com.airbnb.mvrx.args
 import com.google.android.material.dialog.MaterialAlertDialogBuilder
 import im.vector.app.R
+import im.vector.app.core.extensions.crawlCausesFor
 import im.vector.app.databinding.FragmentFtueLoginCaptchaBinding
 import im.vector.app.databinding.ViewStubWebviewBinding
 import im.vector.app.features.onboarding.OnboardingAction
@@ -102,16 +103,4 @@ private fun ViewStub.inflateWebView(onError: (Throwable) -> Unit) {
     }
 }
 
-private fun Throwable?.crawlCausesFor(predicate: (Throwable) -> Boolean): Boolean {
-    return when {
-        this == null -> false
-        else -> {
-            when (predicate(this)) {
-                true -> true
-                else -> this.cause.crawlCausesFor(predicate)
-            }
-        }
-    }
-}
-
 private class MissingWebViewException(cause: Throwable) : IllegalStateException("Failed to load WebView provider: No WebView installed", cause)