use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.
the class WrapperSdkExceptionManagerAndroidTest method saveWrapperException.
@Test
public void saveWrapperException() throws java.lang.Exception {
class ErrorData {
private byte[] data;
private UUID id;
}
ErrorData errorA = new ErrorData();
errorA.data = new byte[] {};
ErrorData errorB = new ErrorData();
errorB.data = null;
ErrorData errorC = new ErrorData();
errorC.data = new byte[] { 'd', 'a', 't', 'a' };
ErrorData[] errors = new ErrorData[] { errorA, errorB, errorC };
for (ErrorData error : errors) {
/* Reset crash state as only 1 crash is saved per process life time. */
startFresh();
/* Save crash. */
error.id = WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), error.data);
byte[] loadedData = WrapperSdkExceptionManager.loadWrapperExceptionData(error.id);
if (error.data == null) {
assertNull(loadedData);
continue;
}
assertNotNull(loadedData);
for (int i = 0; i < error.data.length; ++i) {
assertEquals(error.data[i], loadedData[i]);
}
}
/* Even after deleting errorA, it should exist in memory - so, we can still load it. */
WrapperSdkExceptionManager.deleteWrapperExceptionData(errorA.id);
byte[] loadedDataA = WrapperSdkExceptionManager.loadWrapperExceptionData(errorA.id);
assertNotNull(loadedDataA);
for (int i = 0; i < errorA.data.length; ++i) {
assertEquals(errorA.data[i], loadedDataA[i]);
}
/* Try to load data bypassing the cache. */
WrapperSdkExceptionManager.sWrapperExceptionDataContainer.clear();
byte[] loadedDataC = WrapperSdkExceptionManager.loadWrapperExceptionData(errorC.id);
assertNotNull(loadedDataC);
for (int i = 0; i < errorC.data.length; ++i) {
assertEquals(errorC.data[i], loadedDataC[i]);
}
/* Save another crash without reset: will be ignored as only 1 crash per process. */
assertNull(WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[] { 'e' }));
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project mobile-center-sdk-android by Microsoft.
the class Crashes method initialize.
private void initialize() {
boolean enabled = isInstanceEnabled();
mInitializeTimestamp = enabled ? System.currentTimeMillis() : -1;
if (!enabled) {
if (mUncaughtExceptionHandler != null) {
mUncaughtExceptionHandler.unregister();
mUncaughtExceptionHandler = null;
}
} else {
/* Register Java crash handler. */
mUncaughtExceptionHandler = new UncaughtExceptionHandler();
mUncaughtExceptionHandler.register();
/* Convert minidump files to App Center crash files. */
for (File logFile : ErrorLogHelper.getNewMinidumpFiles()) {
/* Create missing files from the native crash that we detected. */
AppCenterLog.debug(LOG_TAG, "Process pending minidump file: " + logFile);
long minidumpDate = logFile.lastModified();
File dest = new File(ErrorLogHelper.getPendingMinidumpDirectory(), logFile.getName());
NativeException nativeException = new NativeException();
Exception modelException = new Exception();
modelException.setType("minidump");
modelException.setWrapperSdkName(Constants.WRAPPER_SDK_NAME_NDK);
modelException.setStackTrace(dest.getPath());
ManagedErrorLog errorLog = new ManagedErrorLog();
errorLog.setException(modelException);
errorLog.setTimestamp(new Date(minidumpDate));
errorLog.setFatal(true);
errorLog.setId(UUID.randomUUID());
/* 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("");
/*
* TODO device properties are read after restart contrary to Java crashes.
* We should have a device property history like we did for session to fix that issue.
* The main issue with the current code is that app version can change between crash and reporting.
*/
try {
errorLog.setDevice(DeviceInfoHelper.getDeviceInfo(mContext));
errorLog.getDevice().setWrapperSdkName(Constants.WRAPPER_SDK_NAME_NDK);
saveErrorLogFiles(nativeException, errorLog);
if (!logFile.renameTo(dest)) {
throw new IOException("Failed to move file");
}
} catch (java.lang.Exception e) {
// noinspection ResultOfMethodCallIgnored
logFile.delete();
removeAllStoredErrorLogFiles(errorLog.getId());
AppCenterLog.error(LOG_TAG, "Failed to process new minidump file: " + logFile, e);
}
}
/* Check last session crash. */
File logFile = ErrorLogHelper.getLastErrorLogFile();
if (logFile != null) {
AppCenterLog.debug(LOG_TAG, "Processing crash report for the last session.");
String logFileContents = StorageHelper.InternalStorage.read(logFile);
if (logFileContents == null) {
AppCenterLog.error(LOG_TAG, "Error reading last session error log.");
} else {
try {
ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logFileContents);
mLastSessionErrorReport = buildErrorReport(log);
AppCenterLog.debug(LOG_TAG, "Processed crash report for the last session.");
} catch (JSONException e) {
AppCenterLog.error(LOG_TAG, "Error parsing last session error log.", e);
}
}
}
}
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.
the class ErrorLogHelperTest method createErrorLog.
@Test
public void createErrorLog() throws java.lang.Exception {
/* Dummy coverage of utils class. */
new ErrorLogHelper();
/* Mock base. */
Context mockContext = mock(Context.class);
when(Process.myPid()).thenReturn(123);
Date logTimestamp = new Date(1000L);
whenNew(Date.class).withNoArguments().thenReturn(logTimestamp);
whenNew(Date.class).withArguments(anyLong()).thenAnswer(new Answer<Date>() {
@Override
public Date answer(InvocationOnMock invocation) throws Throwable {
return new Date((Long) invocation.getArguments()[0]);
}
});
/* Mock device. */
Device mockDevice = mock(Device.class);
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mockDevice);
/* Mock process name. */
ActivityManager activityManager = mock(ActivityManager.class);
RunningAppProcessInfo runningAppProcessInfo = new RunningAppProcessInfo(null, 0, null);
runningAppProcessInfo.pid = 123;
runningAppProcessInfo.processName = "right.process";
when(mockContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn(activityManager);
when(activityManager.getRunningAppProcesses()).thenReturn(Arrays.asList(mock(RunningAppProcessInfo.class), runningAppProcessInfo));
/* Mock architecture. */
TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", 23);
TestUtils.setInternalState(Build.class, "SUPPORTED_ABIS", new String[] { "armeabi-v7a", "arm" });
/* Test. */
long launchTimeStamp = 2000;
ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new RuntimeException(new IOException(new TestCrashException())), java.lang.Thread.getAllStackTraces(), launchTimeStamp);
assertNotNull(errorLog);
assertNotNull(errorLog.getId());
assertEquals(logTimestamp, errorLog.getTimestamp());
assertEquals(mockDevice, errorLog.getDevice());
assertEquals(Integer.valueOf(123), errorLog.getProcessId());
assertEquals("right.process", errorLog.getProcessName());
assertNull(errorLog.getParentProcessId());
assertNull(errorLog.getParentProcessName());
assertEquals("armeabi-v7a", errorLog.getArchitecture());
assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
assertEquals(Boolean.TRUE, errorLog.getFatal());
assertEquals(launchTimeStamp, errorLog.getAppLaunchTimestamp().getTime());
/* Check first exception. */
Exception topException = errorLog.getException();
sanityCheck(topException);
assertEquals(RuntimeException.class.getName(), topException.getType());
assertNotNull(topException.getMessage());
assertNotNull(topException.getInnerExceptions());
assertEquals(1, topException.getInnerExceptions().size());
/* Check second exception. */
Exception middleException = topException.getInnerExceptions().get(0);
sanityCheck(middleException);
assertEquals(IOException.class.getName(), middleException.getType());
assertNotNull(middleException.getInnerExceptions());
assertEquals(1, middleException.getInnerExceptions().size());
/* Check third exception. */
Exception rootCauseException = middleException.getInnerExceptions().get(0);
sanityCheck(rootCauseException);
assertEquals(TestCrashException.class.getName(), rootCauseException.getType());
assertNotNull(rootCauseException.getMessage());
assertNull(rootCauseException.getInnerExceptions());
/* Check threads. */
assertNotNull(errorLog.getThreads());
assertEquals(java.lang.Thread.getAllStackTraces().size(), errorLog.getThreads().size());
for (Thread thread : errorLog.getThreads()) {
assertNotNull(thread);
assertTrue(thread.getId() > 0);
assertNotNull(thread.getName());
assertNotNull(thread.getFrames());
for (StackFrame frame : thread.getFrames()) {
assertNotNull(frame);
assertNotNull(frame.getClassName());
assertNotNull(frame.getMethodName());
}
}
}
use of com.microsoft.appcenter.crashes.ingestion.models.Exception in project AppCenter-SDK-Android by Microsoft.
the class Crashes method initialize.
private void initialize() {
boolean enabled = isInstanceEnabled();
mInitializeTimestamp = enabled ? System.currentTimeMillis() : -1;
if (!enabled) {
if (mUncaughtExceptionHandler != null) {
mUncaughtExceptionHandler.unregister();
mUncaughtExceptionHandler = null;
}
} else {
/* Register Java crash handler. */
mUncaughtExceptionHandler = new UncaughtExceptionHandler();
mUncaughtExceptionHandler.register();
/* Convert minidump files to App Center crash files. */
for (File logFile : ErrorLogHelper.getNewMinidumpFiles()) {
/* Create missing files from the native crash that we detected. */
AppCenterLog.debug(LOG_TAG, "Process pending minidump file: " + logFile);
long minidumpDate = logFile.lastModified();
File dest = new File(ErrorLogHelper.getPendingMinidumpDirectory(), logFile.getName());
NativeException nativeException = new NativeException();
Exception modelException = new Exception();
modelException.setType("minidump");
modelException.setWrapperSdkName(Constants.WRAPPER_SDK_NAME_NDK);
modelException.setStackTrace(dest.getPath());
ManagedErrorLog errorLog = new ManagedErrorLog();
errorLog.setException(modelException);
errorLog.setTimestamp(new Date(minidumpDate));
errorLog.setFatal(true);
errorLog.setId(UUID.randomUUID());
/* 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("");
/*
* TODO device properties are read after restart contrary to Java crashes.
* We should have a device property history like we did for session to fix that issue.
* The main issue with the current code is that app version can change between crash and reporting.
*/
try {
errorLog.setDevice(DeviceInfoHelper.getDeviceInfo(mContext));
errorLog.getDevice().setWrapperSdkName(Constants.WRAPPER_SDK_NAME_NDK);
saveErrorLogFiles(nativeException, errorLog);
if (!logFile.renameTo(dest)) {
throw new IOException("Failed to move file");
}
} catch (java.lang.Exception e) {
// noinspection ResultOfMethodCallIgnored
logFile.delete();
removeAllStoredErrorLogFiles(errorLog.getId());
AppCenterLog.error(LOG_TAG, "Failed to process new minidump file: " + logFile, e);
}
}
/* Check last session crash. */
File logFile = ErrorLogHelper.getLastErrorLogFile();
if (logFile != null) {
AppCenterLog.debug(LOG_TAG, "Processing crash report for the last session.");
String logFileContents = StorageHelper.InternalStorage.read(logFile);
if (logFileContents == null) {
AppCenterLog.error(LOG_TAG, "Error reading last session error log.");
} else {
try {
ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logFileContents);
mLastSessionErrorReport = buildErrorReport(log);
AppCenterLog.debug(LOG_TAG, "Processed crash report for the last session.");
} catch (JSONException e) {
AppCenterLog.error(LOG_TAG, "Error parsing last session error log.", e);
}
}
}
}
}
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;
for (Throwable cause = t; cause != null; cause = cause.getCause()) {
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;
}
return topException;
}
Aggregations