Created
July 5, 2016 08:43
-
-
Save LouisCAD/6e5f013b9cca11abf8bbf0e7c39d8476 to your computer and use it in GitHub Desktop.
Utilities to detect problems during debug without exposing potential crashes in production
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
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import com.example.app.BuildConfig; | |
import timber.log.Timber; | |
/** | |
* Utils for debugging. | |
*/ | |
public enum DebugUtils { | |
; | |
public static boolean assertThat(boolean expression) { | |
if (expression) return true; | |
final AssertionError ae = new AssertionError(); | |
if (BuildConfig.DEBUG) throw ae; | |
else Timber.wtf(ae, null); | |
return false; | |
} | |
/** | |
* | |
* @param expression must be true to pass. | |
* @param message the unformatted String passed to Timber logging if assertion fails. | |
* @param args the args for the unformatted message. | |
* @return the passed expression | |
*/ | |
public static boolean assertThat(boolean expression, @NonNull String message, Object... args) { | |
if (expression) return true; | |
final AssertionError ae = new AssertionError(); | |
if (BuildConfig.DEBUG) throw ae; | |
else Timber.wtf(ae, message, args); | |
return false; | |
} | |
public static void onIllegalState(@Nullable String message) throws IllegalStateException { | |
final IllegalStateException e = new IllegalStateException(message); | |
if (BuildConfig.DEBUG) throw e; | |
else Timber.wtf(e, null); | |
} | |
public static void onIllegalState() throws IllegalStateException { | |
onIllegalState(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment