validation of API collections logging fixed
This commit is contained in:
parent
8bff940680
commit
ba7f93f9db
|
|
@ -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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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<StackTraceElement> 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue