From ba7f93f9db3fe3bb8af641b66bffec0305ebb243 Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Fri, 13 Jan 2017 17:33:39 +0300 Subject: [PATCH] validation of API collections logging fixed --- .../java/ru/touchin/templates/ApiModel.java | 23 ++++++++++++++-- .../java/ru/touchin/templates/TouchinApp.java | 27 ++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/ru/touchin/templates/ApiModel.java b/src/main/java/ru/touchin/templates/ApiModel.java index defea04..d0fe865 100644 --- a/src/main/java/ru/touchin/templates/ApiModel.java +++ b/src/main/java/ru/touchin/templates/ApiModel.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Iterator; import ru.touchin.roboswag.core.log.Lc; +import ru.touchin.roboswag.core.log.LcGroup; /** * Created by Gavriil Sitnikov on 11/08/2016. @@ -34,6 +35,11 @@ import ru.touchin.roboswag.core.log.Lc; */ public abstract class ApiModel { + /** + * Logging group to log API validation errors. + */ + public static final LcGroup API_VALIDATION_LC_GROUP = new LcGroup("API_VALIDATION"); + /** * Validates list of objects. Use it if objects in list extends {@link ApiModel}. * @@ -63,14 +69,14 @@ public abstract class ApiModel { throw exception; case EXCEPTION_IF_ALL_INVALID: iterator.remove(); - Lc.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position); + API_VALIDATION_LC_GROUP.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position); if (!iterator.hasNext() && !haveValidItem) { throw new ValidationException("Whole list is invalid at " + Lc.getCodePoint(null, 1)); } break; case REMOVE_INVALID_ITEMS: iterator.remove(); - Lc.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position); + API_VALIDATION_LC_GROUP.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position); break; default: Lc.assertion("Unexpected rule " + collectionValidationRule); @@ -81,6 +87,19 @@ public abstract class ApiModel { } } + /** + * Validates collection on emptiness. + * + * @param collection Collection to check; + * @throws ValidationException Exception of validation. + */ + protected static void validateCollectionNotEmpty(@NonNull final Collection collection) + throws ValidationException { + if (collection.isEmpty()) { + throw new ValidationException("List is empty at " + Lc.getCodePoint(null, 1)); + } + } + /** * Validates this object. Override it to write your own logic. * diff --git a/src/main/java/ru/touchin/templates/TouchinApp.java b/src/main/java/ru/touchin/templates/TouchinApp.java index 69044c7..bf137a5 100644 --- a/src/main/java/ru/touchin/templates/TouchinApp.java +++ b/src/main/java/ru/touchin/templates/TouchinApp.java @@ -32,6 +32,8 @@ import com.crashlytics.android.Crashlytics; import net.danlew.android.joda.JodaTimeAndroid; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; import io.fabric.sdk.android.Fabric; @@ -129,17 +131,36 @@ public abstract class TouchinApp extends Application { @Nullable final Throwable throwable) { if (group == UiUtils.UI_LIFECYCLE_LC_GROUP) { crashlytics.core.log(level.getPriority(), tag, message); - } else if (!level.lessThan(LcLevel.ASSERT)) { + } else if (!level.lessThan(LcLevel.ASSERT) + || (group == ApiModel.API_VALIDATION_LC_GROUP && level == LcLevel.ERROR)) { Log.e(tag, message); if (throwable != null) { - crashlytics.core.log(Log.ASSERT, tag, message); + crashlytics.core.log(level.getPriority(), tag, message); crashlytics.core.logException(throwable); } else { - crashlytics.core.logException(new ShouldNotHappenException(tag + ':' + message)); + final ShouldNotHappenException exceptionToLog = new ShouldNotHappenException(tag + ':' + message); + reduceStackTrace(exceptionToLog); + crashlytics.core.logException(exceptionToLog); } } } + private void reduceStackTrace(@NonNull final Throwable throwable) { + final StackTraceElement[] stackTrace = throwable.getStackTrace(); + final List reducedStackTraceList = new ArrayList<>(); + for (int i = stackTrace.length - 1; i >= 0; i--) { + final StackTraceElement stackTraceElement = stackTrace[i]; + if (stackTraceElement.getClassName().contains(getClass().getSimpleName()) + || stackTraceElement.getClassName().contains(LcGroup.class.getName()) + || stackTraceElement.getClassName().contains(Lc.class.getName())) { + break; + } + reducedStackTraceList.add(stackTraceElement); + } + final StackTraceElement[] reducedStackTrace = new StackTraceElement[reducedStackTraceList.size()]; + throwable.setStackTrace(reducedStackTraceList.toArray(reducedStackTrace)); + } + } /**