Search in sources :

Example 46 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrashFailsWithJSONException.

@Test
public void saveWrapperSdkCrashFailsWithJSONException() throws JSONException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenThrow(new JSONException("mock"));
    Crashes.getInstance().setLogSerializer(logSerializer);
    String data = "d";
    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 JSONException;
        }
    }));
    /* 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 JSONException;
        }
    }));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) JSONException(org.json.JSONException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) 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)

Example 47 with Exception

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

the class WrapperSdkExceptionManagerTest method saveWrapperSdkCrash.

@Test
public void saveWrapperSdkCrash() throws JSONException, IOException {
    LogSerializer logSerializer = Mockito.mock(LogSerializer.class);
    when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    String data = "d";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic();
    FileManager.write(any(File.class), eq(data));
    /* We can't do it twice in the same process. */
    data = "e";
    WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data);
    verifyStatic(never());
    FileManager.write(any(File.class), eq(data));
}
Also used : ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Log.getStackTraceString(android.util.Log.getStackTraceString) 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)

Example 48 with Exception

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

the class Crashes method processSingleMinidump.

/**
 * Process the minidump, save an error log with a reference to the minidump, move the minidump file to the 'pending' folder.
 *
 * @param minidumpFile   a file where an ndk crash is saved.
 * @param minidumpFolder a folder that contains device info and a minidump file.
 */
private void processSingleMinidump(File minidumpFile, File minidumpFolder) {
    /* Create missing files from the native crash that we detected. */
    AppCenterLog.debug(LOG_TAG, "Process pending minidump file: " + minidumpFile);
    long minidumpDate = minidumpFile.lastModified();
    File dest = new File(ErrorLogHelper.getPendingMinidumpDirectory(), minidumpFile.getName());
    Exception modelException = new Exception();
    modelException.setType("minidump");
    modelException.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
    modelException.setMinidumpFilePath(dest.getPath());
    ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setException(modelException);
    errorLog.setTimestamp(new Date(minidumpDate));
    errorLog.setFatal(true);
    errorLog.setId(ErrorLogHelper.parseLogFolderUuid(minidumpFolder));
    /* Lookup app launch timestamp in session history. */
    SessionContext.SessionInfo session = SessionContext.getInstance().getSessionAt(minidumpDate);
    if (session != null && session.getAppLaunchTimestamp() <= minidumpDate) {
        errorLog.setAppLaunchTimestamp(new Date(session.getAppLaunchTimestamp()));
    } else {
        /*
             * Fall back to log date if app launch timestamp information lost
             * or in the future compared to crash time.
             * This also covers the case where app launches then crashes within 1s:
             * app launch timestamp would have ms accuracy while minidump file is without
             * ms, in that case we also falls back to log timestamp
             * (this would be same result as truncating ms).
             */
        errorLog.setAppLaunchTimestamp(errorLog.getTimestamp());
    }
    /*
         * TODO The following properties are placeholders because fields are required.
         * They should be removed from schema as not used by server.
         */
    errorLog.setProcessId(0);
    errorLog.setProcessName("");
    try {
        String savedUserId = ErrorLogHelper.getStoredUserInfo(minidumpFolder);
        Device savedDeviceInfo = ErrorLogHelper.getStoredDeviceInfo(minidumpFolder);
        if (savedDeviceInfo == null) {
            /*
                 * Fallback to use device info from the current launch.
                 * It may lead to an incorrect app version being reported.
                 */
            savedDeviceInfo = getDeviceInfo(mContext);
            savedDeviceInfo.setWrapperSdkName(WRAPPER_SDK_NAME_NDK);
        }
        errorLog.setDevice(savedDeviceInfo);
        errorLog.setUserId(savedUserId);
        saveErrorLogFiles(new NativeException(), errorLog);
        if (!minidumpFile.renameTo(dest)) {
            throw new IOException("Failed to move file");
        }
    } catch (java.lang.Exception e) {
        // noinspection ResultOfMethodCallIgnored
        minidumpFile.delete();
        removeAllStoredErrorLogFiles(errorLog.getId());
        AppCenterLog.error(LOG_TAG, "Failed to process new minidump file: " + minidumpFile, e);
    }
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Log.getStackTraceString(android.util.Log.getStackTraceString) IOException(java.io.IOException) Exception(com.microsoft.appcenter.crashes.ingestion.models.Exception) JSONException(org.json.JSONException) NativeException(com.microsoft.appcenter.crashes.model.NativeException) TestCrashException(com.microsoft.appcenter.crashes.model.TestCrashException) IOException(java.io.IOException) Date(java.util.Date) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) SessionContext(com.microsoft.appcenter.utils.context.SessionContext) File(java.io.File) NativeException(com.microsoft.appcenter.crashes.model.NativeException)

Example 49 with Exception

use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project appcenter-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)

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