Skip to content

Instantly share code, notes, and snippets.

@LouisCAD
Created July 5, 2016 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouisCAD/6e5f013b9cca11abf8bbf0e7c39d8476 to your computer and use it in GitHub Desktop.
Save LouisCAD/6e5f013b9cca11abf8bbf0e7c39d8476 to your computer and use it in GitHub Desktop.
Utilities to detect problems during debug without exposing potential crashes in production
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