Search in sources :

Example 1 with HandledErrorLog

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

the class Crashes method queueException.

private synchronized UUID queueException(@NonNull final ExceptionModelBuilder exceptionModelBuilder, Map<String, String> properties, final Iterable<ErrorAttachmentLog> attachments) {
    /* Snapshot userId as early as possible. */
    final String userId = UserIdContext.getInstance().getUserId();
    final UUID errorId = UUID.randomUUID();
    final Map<String, String> validatedProperties = ErrorLogHelper.validateProperties(properties, "HandledError");
    post(new Runnable() {

        @Override
        public void run() {
            /* First send the handled error. */
            HandledErrorLog errorLog = new HandledErrorLog();
            errorLog.setId(errorId);
            errorLog.setUserId(userId);
            errorLog.setException(exceptionModelBuilder.buildExceptionModel());
            errorLog.setProperties(validatedProperties);
            mChannel.enqueue(errorLog, ERROR_GROUP, Flags.DEFAULTS);
            /* Then attachments if any. */
            sendErrorAttachment(errorId, attachments);
        }
    });
    return errorId;
}
Also used : HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Log.getStackTraceString(android.util.Log.getStackTraceString) UUID(java.util.UUID)

Example 2 with HandledErrorLog

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

the class HandledErrorTest method trackExceptionForWrapperSdk.

@Test
public void trackExceptionForWrapperSdk() {
    StackFrame frame = new StackFrame();
    frame.setClassName("1");
    frame.setFileName("2");
    frame.setLineNumber(3);
    frame.setMethodName("4");
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
    exception.setType("5");
    exception.setMessage("6");
    exception.setFrames(singletonList(frame));
    mCrashes = Crashes.getInstance();
    mChannel = mock(Channel.class);
    WrapperSdkExceptionManager.trackException(exception, null, null);
    verify(mChannel, never()).enqueue(any(Log.class), eq(mCrashes.getGroupName()), anyInt());
    mCrashes.onStarting(mAppCenterHandler);
    mCrashes.onStarted(mock(Context.class), mChannel, "", null, true);
    WrapperSdkExceptionManager.trackException(exception, null, null);
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException());
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    }, null);
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            for (int i = 0; i < 30; i++) {
                put("valid" + i, "valid");
            }
        }
    }, null);
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 20;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    reset(mChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    }, null);
    verify(mChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (exception.equals((errorLog.getException()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), eq(mCrashes.getGroupName()), eq(DEFAULTS));
}
Also used : Context(android.content.Context) UserIdContext(com.microsoft.appcenter.utils.context.UserIdContext) 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) Channel(com.microsoft.appcenter.channel.Channel) 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) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with HandledErrorLog

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

the class HandledErrorTest method trackErrorWithEverything.

@Test
public void trackErrorWithEverything() {
    /* If we start crashes. */
    startCrashes();
    /* And set a userId. */
    UserIdContext.getInstance().setUserId("omega");
    /* When we track error. */
    ErrorAttachmentLog textAttachment = ErrorAttachmentLog.attachmentWithText("text", null);
    ErrorAttachmentLog binaryAttachment = ErrorAttachmentLog.attachmentWithBinary("hello".getBytes(), "hello.so", "application/octet-stream");
    HashMap<String, String> properties = new HashMap<String, String>() {

        {
            put("a", "b");
        }
    };
    Crashes.trackError(EXCEPTION, properties, Arrays.asList(textAttachment, binaryAttachment));
    /* Then we send the handled error. */
    ArgumentCaptor<Log> logs = ArgumentCaptor.forClass(Log.class);
    verify(mChannel, times(3)).enqueue(logs.capture(), eq(mCrashes.getGroupName()), eq(DEFAULTS));
    assertNotNull(logs.getAllValues());
    assertEquals(3, logs.getAllValues().size());
    assertTrue(logs.getAllValues().get(0) instanceof HandledErrorLog);
    HandledErrorLog handledErrorLog = (HandledErrorLog) logs.getAllValues().get(0);
    assertEquals(EXCEPTION.getMessage(), handledErrorLog.getException().getMessage());
    assertEquals(properties, handledErrorLog.getProperties());
    /* Then the attachments. */
    assertSame(textAttachment, logs.getAllValues().get(1));
    assertSame(binaryAttachment, logs.getAllValues().get(2));
    /* We send userId only in the error log. */
    assertEquals("omega", handledErrorLog.getUserId());
    assertNull(logs.getAllValues().get(1).getUserId());
    assertNull(logs.getAllValues().get(2).getUserId());
}
Also used : HashMap(java.util.HashMap) 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) Test(org.junit.Test)

Example 4 with HandledErrorLog

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

the class CrashesTest method trackExceptionForWrapperSdk.

@Test
public void trackExceptionForWrapperSdk() {
    StackFrame frame = new StackFrame();
    frame.setClassName("1");
    frame.setFileName("2");
    frame.setLineNumber(3);
    frame.setMethodName("4");
    final com.microsoft.appcenter.crashes.ingestion.models.Exception exception = new com.microsoft.appcenter.crashes.ingestion.models.Exception();
    exception.setType("5");
    exception.setMessage("6");
    exception.setFrames(singletonList(frame));
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    WrapperSdkExceptionManager.trackException(exception);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException());
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put("valid" + i, "valid");
            }
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && exception.equals(((HandledErrorLog) item).getException()) && ((HandledErrorLog) item).getProperties().size() == 5;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    WrapperSdkExceptionManager.trackException(exception, new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (exception.equals((errorLog.getException()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        String truncatedMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH, '*');
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) StackFrame(com.microsoft.appcenter.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with HandledErrorLog

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

the class CrashesTest method trackException.

@Test
public void trackException() {
    /* Track exception test. */
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", mockChannel);
    Crashes.trackException(EXCEPTION);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage());
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    Crashes.trackException(EXCEPTION, new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 0;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    Crashes.trackException(EXCEPTION, new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put("valid" + i, "valid");
            }
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof HandledErrorLog && EXCEPTION.getMessage().equals(((HandledErrorLog) item).getException().getMessage()) && ((HandledErrorLog) item).getProperties().size() == 5;
        }
    }), eq(crashes.getGroupName()));
    reset(mockChannel);
    final String longerMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    Crashes.trackException(EXCEPTION, new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof HandledErrorLog) {
                HandledErrorLog errorLog = (HandledErrorLog) item;
                if (EXCEPTION.getMessage().equals((errorLog.getException().getMessage()))) {
                    if (errorLog.getProperties().size() == 1) {
                        Map.Entry<String, String> entry = errorLog.getProperties().entrySet().iterator().next();
                        String truncatedMapItem = generateString(ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH, '*');
                        return entry.getKey().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == ErrorLogHelper.MAX_PROPERTY_ITEM_LENGTH;
                    }
                }
            }
            return false;
        }
    }), eq(crashes.getGroupName()));
    HandledErrorLog mockLog = mock(HandledErrorLog.class);
    CrashesListener mockListener = mock(CrashesListener.class);
    crashes.setInstanceListener(mockListener);
    /* Crashes callback test for trackException. */
    crashes.getChannelListener().onBeforeSending(mockLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(mockLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(mockLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
    ErrorAttachmentLog attachmentLog = mock(ErrorAttachmentLog.class);
    crashes.getChannelListener().onBeforeSending(attachmentLog);
    verify(mockListener, never()).onBeforeSending(any(ErrorReport.class));
    crashes.getChannelListener().onSuccess(attachmentLog);
    verify(mockListener, never()).onSendingSucceeded(any(ErrorReport.class));
    crashes.getChannelListener().onFailure(attachmentLog, EXCEPTION);
    verify(mockListener, never()).onSendingFailed(any(ErrorReport.class), eq(EXCEPTION));
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) Channel(com.microsoft.appcenter.channel.Channel) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) ErrorReport(com.microsoft.appcenter.crashes.model.ErrorReport) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)11 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)10 Test (org.junit.Test)10 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)9 Matchers.anyString (org.mockito.Matchers.anyString)9 HashMap (java.util.HashMap)8 Log (com.microsoft.appcenter.ingestion.models.Log)7 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)7 Map (java.util.Map)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Context (android.content.Context)5 Channel (com.microsoft.appcenter.channel.Channel)5 SessionContext (com.microsoft.appcenter.SessionContext)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Exception (com.microsoft.appcenter.crashes.ingestion.models.Exception)3 StackFrame (com.microsoft.appcenter.crashes.ingestion.models.StackFrame)3 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)3 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)2 NativeException (com.microsoft.appcenter.crashes.model.NativeException)2 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)2