-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite ExceptionUtils methods as extension functions.
- Loading branch information
1 parent
13e7d2e
commit 37cc6d1
Showing
11 changed files
with
140 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
@file:JvmName("ExceptionUtils") | ||
|
||
package org.schabi.newpipe.ktx | ||
|
||
import java.io.IOException | ||
import java.io.InterruptedIOException | ||
|
||
/** | ||
* @return if throwable is related to Interrupted exceptions, or one of its causes is. | ||
*/ | ||
val Throwable.isInterruptedCaused: Boolean | ||
get() = hasExactCause(InterruptedIOException::class.java, InterruptedException::class.java) | ||
|
||
/** | ||
* @return if throwable is related to network issues, or one of its causes is. | ||
*/ | ||
val Throwable.isNetworkRelated: Boolean | ||
get() = hasAssignableCause(IOException::class.java) | ||
|
||
/** | ||
* Calls [hasCause] with the `checkSubtypes` parameter set to false. | ||
*/ | ||
fun Throwable.hasExactCause(vararg causesToCheck: Class<*>) = hasCause(false, *causesToCheck) | ||
|
||
/** | ||
* Calls [hasCause] with the `checkSubtypes` parameter set to true. | ||
*/ | ||
fun Throwable?.hasAssignableCause(vararg causesToCheck: Class<*>) = hasCause(true, *causesToCheck) | ||
|
||
/** | ||
* Check if the throwable has some cause from the causes to check, or is itself in it. | ||
* | ||
* If `checkIfAssignable` is true, not only the exact type will be considered equals, but also its subtypes. | ||
* | ||
* @param checkSubtypes if subtypes are also checked. | ||
* @param causesToCheck an array of causes to check. | ||
* | ||
* @see Class.isAssignableFrom | ||
*/ | ||
tailrec fun Throwable?.hasCause(checkSubtypes: Boolean, vararg causesToCheck: Class<*>): Boolean { | ||
if (this == null) { | ||
return false | ||
} | ||
|
||
// Check if throwable is a subtype of any of the causes to check | ||
causesToCheck.forEach { causeClass -> | ||
if (checkSubtypes) { | ||
if (causeClass.isAssignableFrom(this.javaClass)) { | ||
return true | ||
} | ||
} else { | ||
if (causeClass == this.javaClass) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
val currentCause: Throwable? = cause | ||
// Check if cause is not pointing to the same instance, to avoid infinite loops. | ||
if (this !== currentCause) { | ||
return currentCause.hasCause(checkSubtypes, *causesToCheck) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 0 additions & 86 deletions
86
app/src/main/java/org/schabi/newpipe/util/ExceptionUtils.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/test/java/org/schabi/newpipe/ktx/ThrowableExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.schabi.newpipe.ktx | ||
|
||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import java.io.IOException | ||
import java.io.InterruptedIOException | ||
import java.net.SocketException | ||
import javax.net.ssl.SSLException | ||
|
||
class ThrowableExtensionsTest { | ||
@Test fun `assignable causes`() { | ||
assertTrue(Throwable().hasAssignableCause(Throwable::class.java)) | ||
assertTrue(Exception().hasAssignableCause(Exception::class.java)) | ||
assertTrue(IOException().hasAssignableCause(Exception::class.java)) | ||
|
||
assertTrue(IOException().hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(SocketException()).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException()).hasAssignableCause(RuntimeException::class.java)) | ||
assertTrue(Exception(Exception(IOException())).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(RuntimeException::class.java)) | ||
|
||
assertTrue(IllegalStateException().hasAssignableCause(Throwable::class.java)) | ||
assertTrue(IllegalStateException().hasAssignableCause(Exception::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(InterruptedIOException::class.java)) | ||
} | ||
|
||
@Test fun `no assignable causes`() { | ||
assertFalse(Throwable().hasAssignableCause(Exception::class.java)) | ||
assertFalse(Exception().hasAssignableCause(IOException::class.java)) | ||
assertFalse(Exception(IllegalStateException()).hasAssignableCause(IOException::class.java)) | ||
assertFalse(Exception(NullPointerException()).hasAssignableCause(IOException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(Exception()))).hasAssignableCause(IOException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasAssignableCause(InterruptedIOException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasAssignableCause(InterruptedException::class.java)) | ||
} | ||
|
||
@Test fun `exact causes`() { | ||
assertTrue(Throwable().hasExactCause(Throwable::class.java)) | ||
assertTrue(Exception().hasExactCause(Exception::class.java)) | ||
|
||
assertTrue(IOException().hasExactCause(IOException::class.java)) | ||
assertTrue(Exception(SocketException()).hasExactCause(SocketException::class.java)) | ||
assertTrue(Exception(Exception(IOException())).hasExactCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(IOException()))).hasExactCause(IOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause(SocketException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(SSLException("IO")))).hasExactCause(SSLException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(InterruptedIOException::class.java)) | ||
assertTrue(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(IllegalStateException::class.java)) | ||
} | ||
|
||
@Test fun `no exact causes`() { | ||
assertFalse(Throwable().hasExactCause(Exception::class.java)) | ||
assertFalse(Exception().hasExactCause(Throwable::class.java)) | ||
|
||
assertFalse(SocketException().hasExactCause(IOException::class.java)) | ||
assertFalse(IllegalStateException().hasExactCause(RuntimeException::class.java)) | ||
assertFalse(Exception(SocketException()).hasExactCause(IOException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(IOException()))).hasExactCause(RuntimeException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(SocketException()))).hasExactCause(IOException::class.java)) | ||
assertFalse(Exception(IllegalStateException(Exception(InterruptedIOException()))).hasExactCause(IOException::class.java)) | ||
} | ||
} |
69 changes: 0 additions & 69 deletions
69
app/src/test/java/org/schabi/newpipe/util/ExceptionUtilsTest.kt
This file was deleted.
Oops, something went wrong.