Search in sources :

Example 41 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelper method getModelExceptionFromThrowable.

@NonNull
public static Exception getModelExceptionFromThrowable(@NonNull Throwable t) {
    Exception topException = null;
    Exception parentException = null;
    List<Throwable> causeChain = new LinkedList<>();
    for (Throwable cause = t; cause != null; cause = cause.getCause()) {
        causeChain.add(cause);
    }
    if (causeChain.size() > CAUSE_LIMIT) {
        AppCenterLog.warn(Crashes.LOG_TAG, "Crash causes truncated from " + causeChain.size() + " to " + CAUSE_LIMIT + " causes.");
        causeChain.subList(CAUSE_LIMIT_HALF, causeChain.size() - CAUSE_LIMIT_HALF).clear();
    }
    for (Throwable cause : causeChain) {
        Exception exception = new Exception();
        exception.setType(cause.getClass().getName());
        exception.setMessage(cause.getMessage());
        exception.setFrames(getModelFramesFromStackTrace(cause));
        if (topException == null) {
            topException = exception;
        } else {
            parentException.setInnerExceptions(Collections.singletonList(exception));
        }
        parentException = exception;
    }
    // noinspection ConstantConditions
    return topException;
}
Also used : Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) NonNull(androidx.annotation.NonNull)

Example 42 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method truncateCauses.

@Test
public void truncateCauses() {
    RuntimeException e = new RuntimeException();
    for (int i = 0; i < 32; i++) {
        e = new RuntimeException(Integer.valueOf(i).toString(), e);
    }
    int depth = 1;
    Exception model = ErrorLogHelper.getModelExceptionFromThrowable(e);
    while (model.getInnerExceptions() != null && (model = model.getInnerExceptions().get(0)) != null) {
        depth++;
    }
    assertEquals(ErrorLogHelper.CAUSE_LIMIT, depth);
}
Also used : Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 43 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project appcenter-sdk-android by microsoft.

the class ErrorLogHelperTest method truncateCauses.

@Test
public void truncateCauses() {
    RuntimeException e = new RuntimeException();
    for (int i = 0; i < 32; i++) {
        e = new RuntimeException(Integer.valueOf(i).toString(), e);
    }
    int depth = 1;
    Exception model = ErrorLogHelper.getModelExceptionFromThrowable(e);
    while (model.getInnerExceptions() != null && (model = model.getInnerExceptions().get(0)) != null) {
        depth++;
    }
    assertEquals(ErrorLogHelper.CAUSE_LIMIT, depth);
}
Also used : Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 44 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project appcenter-sdk-android by microsoft.

the class HandledErrorTest method trackExceptionWithOneAttachmentFromWrapper.

@Test
public void trackExceptionWithOneAttachmentFromWrapper() {
    /* If we start crashes. */
    startCrashes();
    /* When we track error with an attachment. */
    ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("text", null);
    Exception exception = new Exception();
    String errorId = WrapperSdkExceptionManager.trackException(exception, null, Collections.singleton(textAttachment));
    assertNotNull(errorId);
    /* Then we send the handled error. */
    ArgumentCaptor<Log> log = ArgumentCaptor.forClass(Log.class);
    verify(mChannel, times(2)).enqueue(log.capture(), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    assertNotNull(log.getAllValues());
    assertEquals(2, log.getAllValues().size());
    assertTrue(log.getAllValues().get(0) instanceof HandledErrorLog);
    HandledErrorLog handledErrorLog = (HandledErrorLog) log.getAllValues().get(0);
    assertEquals(exception, handledErrorLog.getException());
    assertEquals(errorId, String.valueOf(handledErrorLog.getId()));
    /* Then the attachment. */
    assertSame(textAttachment, log.getAllValues().get(1));
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) Test(org.junit.Test)

Example 45 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project appcenter-sdk-android by microsoft.

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithIOExceptionAfterLog.

@Test
public void saveWrapperSdkCrashFailsWithIOExceptionAfterLog() throws IOException, JSONException {
    String data = "d";
    doThrow(new IOException()).when(FileManager.class);
    FileManager.write(any(File.class), eq(data));
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
    /* Second call is ignored. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    /* No more error. */
    verifyStatic();
    AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof IOException;
        }
    }));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) IOException(java.io.IOException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)49 Test (org.junit.Test)41 IOException (java.io.IOException)38 JSONException (org.json.JSONException)38 Matchers.anyString (org.mockito.Matchers.anyString)33 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 Log.getStackTraceString (android.util.Log.getStackTraceString)27 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)24 File (java.io.File)23 Log (com.microsoft.appcenter.ingestion.models.Log)12 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)12 ArgumentMatcher (org.mockito.ArgumentMatcher)12 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)11 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)9 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)9 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)9 Date (java.util.Date)8 Context (android.content.Context)6 StackFrame (com.microsoft.appcenter.crashes.ingestion.models.StackFrame)6