-
-
Notifications
You must be signed in to change notification settings - Fork 778
Introduce FailFast utils to provide offensive programing #5373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
284e3ca
Introduce FailFast utils
TimoPtr 72b3a05
FailFast on unknown sentry issues
TimoPtr 9cd59b4
Rollback unwanted changes in DevPlayground
TimoPtr e031084
Move handlers in common/main instead of specific folder
TimoPtr cea06b4
Revert unwanted changes
TimoPtr b321625
Fix test after signature change
TimoPtr 8c562cb
Merge remote-tracking branch 'origin/main' into feature/fail_fast
TimoPtr f811b50
Apply suggestions
TimoPtr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
3 changes: 3 additions & 0 deletions
3
...src/debug/kotlin/io/homeassistant/companion/android/common/util/DefaultFailFastHandler.kt
This file contains hidden or 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,3 @@ | ||
package io.homeassistant.companion.android.common.util | ||
|
||
val DefaultFailFastHandler = CrashFailFastHandler |
This file contains hidden or 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
61 changes: 61 additions & 0 deletions
61
common/src/main/kotlin/io/homeassistant/companion/android/common/util/FailFast.kt
This file contains hidden or 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,61 @@ | ||
package io.homeassistant.companion.android.common.util | ||
|
||
import io.homeassistant.companion.android.common.util.FailFast.setHandler | ||
|
||
/** | ||
* A handler for [FailFast] exceptions. | ||
* | ||
* Implement this interface to define a custom handler if you want to do something different than [DefaultFailFastHandler]. | ||
* Don't forget to register the handler in [FailFast.setHandler]. | ||
*/ | ||
interface FailFastHandler { | ||
fun handleException(exception: Exception, additionalMessage: String? = null) | ||
} | ||
|
||
private class FailFastException : Exception { | ||
constructor(message: String) : super(message) | ||
constructor(exception: Throwable) : super(exception) | ||
|
||
init { | ||
// We remove any reference to FailFast from the stack trace to make it easier to find the root cause | ||
stackTrace = stackTrace.filterNot { it.className == FailFast::class.java.name }.toTypedArray() | ||
} | ||
} | ||
|
||
/** | ||
* A utility object for implementing the "fail fast" [principle](https://en.wikipedia.org/wiki/Fail-fast_system). | ||
* | ||
* This object provides methods to check conditions and handle exceptions, | ||
* allowing to identify and address issues sooner in the development lifecycle. | ||
* | ||
* By default, it uses [DefaultFailFastHandler] to log exceptions. This behavior can be | ||
* customized by providing a different [FailFastHandler] implementation using [setHandler]. | ||
* | ||
* [DefaultFailFastHandler] behavior is different based on the build target debug or release. | ||
*/ | ||
object FailFast { | ||
private var handler: FailFastHandler = DefaultFailFastHandler | ||
|
||
fun setHandler(handler: FailFastHandler) { | ||
this.handler = handler | ||
} | ||
|
||
fun failWhen(condition: Boolean, message: () -> String) { | ||
TimoPtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (condition) { | ||
handler.handleException(FailFastException(message())) | ||
} | ||
} | ||
|
||
fun failOnCatch(message: () -> String? = { null }, block: () -> Unit) { | ||
failOnCatch<Unit>(message, Unit, block) | ||
} | ||
|
||
fun <T> failOnCatch(message: () -> String? = { null }, fallback: T, block: () -> T): T { | ||
return try { | ||
block() | ||
} catch (e: Throwable) { | ||
handler.handleException(FailFastException(e), message()) | ||
fallback | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
common/src/main/kotlin/io/homeassistant/companion/android/common/util/FailFastHandlers.kt
This file contains hidden or 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,68 @@ | ||
package io.homeassistant.companion.android.common.util | ||
|
||
import kotlin.system.exitProcess | ||
import timber.log.Timber | ||
|
||
private const val HEADER = """ | ||
████████████████████████████████████████████████████████████████ | ||
██ ██ | ||
██ !!! CRITICAL FAILURE: FAIL-FAST !!! ██ | ||
██ ██ | ||
████████████████████████████████████████████████████████████████ | ||
""" | ||
|
||
private const val SEPARATOR = """----------------------------------------------------------------""" | ||
|
||
object CrashFailFastHandler : FailFastHandler { | ||
override fun handleException(exception: Exception, additionalMessage: String?) { | ||
Timber.e( | ||
exception, | ||
buildString { | ||
appendLine(HEADER.trimIndent()) | ||
appendLine() | ||
appendLine( | ||
""" | ||
An unrecoverable error has occurred, and the FailFast mechanism | ||
has been triggered. The application cannot continue and will now exit. | ||
|
||
ACTION REQUIRED: This error must be investigated and resolved. | ||
Review the accompanying stack trace for details. | ||
""".trimIndent(), | ||
) | ||
appendLine(SEPARATOR) | ||
additionalMessage?.let { | ||
appendLine() | ||
appendLine(it) | ||
appendLine(SEPARATOR) | ||
TimoPtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
appendLine() | ||
} | ||
}, | ||
) | ||
exitProcess(1) | ||
} | ||
} | ||
|
||
object LogOnlyFailFastHandler : FailFastHandler { | ||
override fun handleException(exception: Exception, additionalMessage: String?) { | ||
Timber.e( | ||
exception, | ||
buildString { | ||
appendLine(HEADER.trimIndent()) | ||
appendLine() | ||
appendLine( | ||
""" | ||
The exception is ignored to avoid a crash but it should be handled | ||
if you see this please open an issue on github https://github.com/home-assistant/android/issues/new/choose | ||
TimoPtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""".trimIndent(), | ||
) | ||
additionalMessage?.let { | ||
appendLine() | ||
appendLine(it) | ||
appendLine(SEPARATOR) | ||
appendLine() | ||
} | ||
}, | ||
) | ||
// no-op | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...c/release/kotlin/io/homeassistant/companion/android/common/util/DefaultFailFastHandler.kt
This file contains hidden or 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,3 @@ | ||
package io.homeassistant.companion.android.common.util | ||
|
||
val DefaultFailFastHandler = LogOnlyFailFastHandler |
23 changes: 23 additions & 0 deletions
23
common/src/test/kotlin/io/homeassistant/companion/android/common/util/FailFastTest.kt
This file contains hidden or 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,23 @@ | ||
package io.homeassistant.companion.android.common.util | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class FailFastTest { | ||
|
||
@Test | ||
TimoPtr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fun `Given a throw when invoking failOnCatch then properly propagate the error with custom stacktrace`() { | ||
var exceptionCaught: Exception? = null | ||
FailFast.setHandler(object : FailFastHandler { | ||
override fun handleException(exception: Exception, additionalMessage: String?) { | ||
exceptionCaught = exception | ||
} | ||
}) | ||
FailFast.failOnCatch { | ||
throw IllegalArgumentException("expected error") | ||
} | ||
|
||
assertEquals(exceptionCaught?.message, "java.lang.IllegalArgumentException: expected error") | ||
assertEquals(FailFastTest::class.java.name, exceptionCaught?.stackTrace?.first()?.className) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.