Search in sources :

Example 11 with ErrorReport

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();
        }
    });
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) JSONException(org.json.JSONException) UUID(java.util.UUID) File(java.io.File)

Example 12 with ErrorReport

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);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Iterator(java.util.Iterator) JSONException(org.json.JSONException) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File)

Example 13 with ErrorReport

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());
        }
    });
}
Also used : Channel(com.microsoft.azure.mobile.channel.Channel) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 14 with ErrorReport

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());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with ErrorReport

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);
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)22 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 File (java.io.File)15 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 Context (android.content.Context)10 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)8 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)6 JSONException (org.json.JSONException)6 Log (com.microsoft.azure.mobile.ingestion.models.Log)5 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)4 IOException (java.io.IOException)4 ResultCallback (com.microsoft.azure.mobile.ResultCallback)3 Date (java.util.Date)3 Device (com.microsoft.azure.mobile.ingestion.models.Device)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2