use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class Crashes method processPendingErrors.
private void processPendingErrors() {
mHandler.post(new Runnable() {
@Override
public void run() {
for (File logFile : ErrorLogHelper.getStoredErrorLogFiles()) {
if (shouldStopProcessingPendingErrors())
return;
MobileCenterLog.debug(LOG_TAG, "Process pending error file: " + logFile);
String logfileContents = StorageHelper.InternalStorage.read(logFile);
if (logfileContents != null)
try {
ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logfileContents);
UUID id = log.getId();
ErrorReport report = buildErrorReport(log);
if (report == null) {
removeAllStoredErrorLogFiles(id);
} else if (mCrashesListener.shouldProcess(report)) {
MobileCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned true, continue processing log: " + id.toString());
mUnprocessedErrorReports.put(id, mErrorReportCache.get(id));
} else {
MobileCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned false, clean up and ignore log: " + id.toString());
removeAllStoredErrorLogFiles(id);
}
} catch (JSONException e) {
MobileCenterLog.error(LOG_TAG, "Error parsing error log", e);
}
}
if (shouldStopProcessingPendingErrors())
return;
processUserConfirmation();
}
});
}
use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class Crashes method initialize.
private void initialize() {
boolean enabled = isInstanceEnabled();
mInitializeTimestamp = enabled ? SystemClock.elapsedRealtime() : -1;
if (!enabled) {
if (mUncaughtExceptionHandler != null) {
mUncaughtExceptionHandler.unregister();
mUncaughtExceptionHandler = null;
}
} else if (mContext != null && mUncaughtExceptionHandler == null) {
mUncaughtExceptionHandler = new UncaughtExceptionHandler();
mUncaughtExceptionHandler.register();
final File logFile = ErrorLogHelper.getLastErrorLogFile();
if (logFile != null) {
MobileCenterLog.debug(LOG_TAG, "Processing crash report for the last session.");
mCountDownLatch = new CountDownLatch(1);
mHandler.post(new Runnable() {
@Override
public void run() {
String logFileContents = StorageHelper.InternalStorage.read(logFile);
if (logFileContents == null)
MobileCenterLog.error(LOG_TAG, "Error reading last session error log.");
else {
try {
ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logFileContents);
mLastSessionErrorReport = buildErrorReport(log);
MobileCenterLog.debug(LOG_TAG, "Processed crash report for the last session.");
} catch (JSONException e) {
MobileCenterLog.error(LOG_TAG, "Error parsing last session error log.", e);
}
}
mCountDownLatch.countDown();
HandlerUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
/* Call callbacks for getInstanceLastSessionCrashReport(ResultCallback) . */
for (Iterator<ResultCallback<ErrorReport>> iterator = mLastCrashErrorReportCallbacks.iterator(); iterator.hasNext(); ) {
ResultCallback<ErrorReport> callback = iterator.next();
iterator.remove();
callback.onResult(mLastSessionErrorReport);
}
}
});
}
});
}
}
}
use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesAndroidTest method wrapperSdkOverrideLog.
@Test
public void wrapperSdkOverrideLog() throws InterruptedException {
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
Channel channel = mock(Channel.class);
Crashes.getInstance().onStarted(sContext, "", channel);
Crashes.WrapperSdkListener wrapperSdkListener = mock(Crashes.WrapperSdkListener.class);
Crashes.getInstance().setWrapperSdkListener(wrapperSdkListener);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
ManagedErrorLog errorLog = (ManagedErrorLog) invocationOnMock.getArguments()[0];
errorLog.setErrorThreadName("ReplacedErrorThreadName");
WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
return null;
}
}).when(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
final RuntimeException exception = new RuntimeException("mock");
final Thread thread = new Thread() {
@Override
public void run() {
throw exception;
}
};
thread.start();
thread.join();
verify(wrapperSdkListener).onCrashCaptured(notNull(ManagedErrorLog.class));
Crashes.unsetInstance();
Crashes.getInstance().onStarted(sContext, "", channel);
waitForCrashesHandlerTasksToComplete();
Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {
@Override
public void onResult(ErrorReport errorReport) {
assertNotNull(errorReport);
assertEquals("ReplacedErrorThreadName", errorReport.getThreadName());
}
});
}
use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesAlwaysSend.
@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getContentType()).thenReturn("");
when(mockAttachment.getFileName()).thenReturn("");
when(mockAttachment.getData()).thenReturn(new byte[0]);
when(mockAttachment.isValid()).thenReturn(true);
List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
Crashes crashes = Crashes.getInstance();
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(mockListener);
crashes.onStarted(mockContext, "", mockChannel);
verify(mockListener).shouldProcess(report);
verify(mockListener, never()).shouldAwaitUserConfirmation();
verify(mockListener).getErrorAttachments(report);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object log) {
return log.equals(mErrorLog);
}
}), eq(crashes.getGroupName()));
verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
use of com.microsoft.azure.mobile.crashes.model.ErrorReport in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method buildErrorReport.
@Test
public void buildErrorReport() throws IOException, ClassNotFoundException {
ErrorReport errorReport = ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION);
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class)).thenReturn(null);
when(ErrorLogHelper.getErrorReportFromErrorLog(mErrorLog, EXCEPTION)).thenReturn(errorReport);
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(EXCEPTION);
Crashes crashes = Crashes.getInstance();
ErrorReport report = crashes.buildErrorReport(mErrorLog);
assertErrorEquals(mErrorLog, report);
mErrorLog.setId(UUIDUtils.randomUUID());
report = crashes.buildErrorReport(mErrorLog);
assertNull(report);
}
Aggregations