Skip to content

Crash Detection

AppAmbit Crashes automatically records a crash log whenever your application crashes. Initially, the log is saved to the device's local storage. Once the app is reopened, the crash report is sent to AppAmbit, offering valuable insights to help diagnose and fix issues.

Generate a Test Crash

To simplify SDK testing, AppAmbit Crashes provides an API for generating a test crash.

Crashes.GenerateTestCrash();

Swift

Crashes.GenerateTestCrash();

Objective-C

[Crashes generateTestCrash];

Java

Crashes.generateTestCrash();

Kotlin

Crashes.generateTestCrash()

AppambitSdk.generateTestCrash()
generateTestCrash();
Crashes.GenerateTestCrash();
Crashes.GenerateTestCrash();

Handled Errors

AppAmbit also supports tracking non-fatal errors by logging handled exceptions.

Basic Error Logging

try {
    // Your code
} catch (Exception exception) {
    Crashes.LogError(exception);
}

Swift

do {
    throw NSError(domain: "com.appambit", code: -1,
                  userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
} catch let error {
    Crashes.logError(exception: error)
}

Objective-C

@try {
    NSArray *array = @[];
    NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
    NSError *error = [NSError errorWithDomain:exception.name
                                          code:0
                                      userInfo:userInfo];

    [Crashes logErrorWithException:error properties:nil classFqn:nil
                          fileName:nil lineNumber:0 completion:nil];
}

Java

try {
    // Your code
} catch (Exception exception) {
    Crashes.logError(exception);
}

Kotlin

try {
    // Your code
} catch (exception: Exception) {
    Crashes.logError(exception)
}

try {
    throw Exception('Test');
} catch (e, st) {
    await AppambitSdk.logError(
        exception: e,
        stackTrace: st,
    );
}
try {
    throw new Error();
} catch (e) {
    logError({
        stack: e.stack,
        exception: e,
        classFqn: e.constructor.name
    });
}
try {
    // Your code
} catch (Exception exception) {
    Crashes.LogError(exception);
}
try {
    // Your code
} catch (Exception exception) {
    Crashes.LogError(exception);
}

Error Logging with Properties

Besides basic error logging, an application can attach properties to a controlled error report to provide more context. Pass the properties as a map of key-value pairs (strings only).

try {
    // Your code
} catch (Exception exception) {
    var properties = new Dictionary<string, string>
    {
        { "user_data_role", "admin" },
        { "battery_level", "90" }
    };
    Crashes.LogError(exception, properties);
}

Swift

do {
    throw NSError(domain: "com.appambit", code: -1,
                  userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
} catch let error {
    let properties: [String: String] = ["user_id": "1"]
    Crashes.logError(exception: error, properties: properties)
}

Objective-C

@try {
    NSArray *array = @[];
    NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    [props setObject:@"123" forKey:@"userId"];
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
    NSError *error = [NSError errorWithDomain:exception.name code:0 userInfo:userInfo];

    [Crashes logErrorWithException:error properties:props classFqn:nil
                          fileName:nil lineNumber:0 completion:nil];
}

Java

try {
    // Your code
} catch (Exception exception) {
    Map<String, String> properties = new HashMap<>();
    properties.put("user_id", "1");
    Crashes.logError(exception, properties);
}

Kotlin

try {
    // Your code
} catch (exception: Exception) {
    val properties: Map<String, String> = mapOf(
        "user_id" to "1"
    )
    Crashes.logError(exception, properties)
}

try {
    throw Exception('Test with Properties');
} catch (e, st) {
    await AppambitSdk.logError(
        exception: e,
        stackTrace: st,
        properties: <String, String>{'user_id': '1'}
    );
}
try {
    throw new Error();
} catch (e) {
    logError({
        stack: e.stack,
        exception: e,
        classFqn: e.constructor.name,
        properties: {"user_id": "1"}
    });
}
try {
    // Your code
} catch (Exception exception) {
    var properties = new Dictionary<string, string>
    {
        { "user_data_role", "admin" },
        { "battery_level", "90" }
    };
    Crashes.LogError(exception, properties);
}
try {
    // Your code
} catch (Exception exception) {
    var properties = new Dictionary<string, string>
    {
        { "user_data_role", "admin" },
        { "battery_level", "90" }
    };
    Crashes.LogError(exception, properties);
}

Custom Error Messages

Additionally, you can log custom error messages for better visibility during unexpected situations:

try {
    // Your code
} catch (Exception exception) {
    var message = "Error NullPointerException";
    Crashes.LogError(message);
}

Swift

Crashes.logError(message: "This is a test crash")

Objective-C

[Crashes logErrorWithMessage:(@"This is a test crash") properties:nil
                      classFqn:nil exception:nil fileName:nil lineNumber:0
                     createdAt:nil completion:nil];

Java

try {
    // Your code
} catch (Exception exception) {
    String message = "Error NullPointerException";
    Crashes.logError(message);
}

Kotlin

try {
    // Your code
} catch (exception: Exception) {
    val message = "Error NullPointerException"
    Crashes.logError(message)
}

try {
    // Your code
} catch (e, st) {
    final msg = "Error Exception";
    await AppambitSdk.logError(message: msg);
}
logErrorMessage("Test Message LogError")
try {
    // Your code
} catch (Exception exception) {
    var message = "Error NullPointerException";
    Crashes.LogError(message);
}
try {
    // Your code
} catch (Exception exception) {
    var message = "Error NullPointerException";
    Crashes.LogError(message);
}

Error Messages with Properties

Log with both message and properties to get more details about errors:

try {
    // Your code
} catch (Exception exception) {
    var props = new Dictionary<string, string> { { "user_id", "1" } };
    var message = "Error NullPointerException";
    Crashes.LogError(message, props);
}

Swift

do {
    throw NSError(domain: "com.appambit", code: -1,
                  userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
} catch let error {
    let properties: [String: String] = ["user_id": "1"]
    let message = "Error NullPointerException"
    Crashes.logError(message: message, properties: properties, exception: error)
}

Objective-C

@try {
    NSArray *array = @[];
    NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    [props setObject:@"123" forKey:@"userId"];
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
    NSError *error = [NSError errorWithDomain:exception.name code:0 userInfo:userInfo];

    [Crashes logErrorWithMessage:(@"Error ArrayIndex") properties:props classFqn:nil
                       exception:nil fileName:nil lineNumber:0 createdAt:nil completion:nil];
}

Java

try {
    // Your code
} catch (Exception exception) {
    Map<String, String> properties = new HashMap<>();
    properties.put("user_id", "1");
    String message = "Error NullPointerException";
    Crashes.logError(message, properties);
}

Kotlin

try {
    // Your code
} catch (exception: Exception) {
    val properties: Map<String, String> = mapOf(
        "user_id" to "1"
    )
    val message = "Error NullPointerException"
    Crashes.logError(message, properties)
}

try {
    // Your code
} catch (e, st) {
    final msg = "Error Exception";
    await AppambitSdk.logError(
        message: msg,
        properties: <String, String>{'user_id': '1'}
    );
}
try {
    throw new Error();
} catch (e) {
    logError({
        message: "Test Message",
        stack: e.stack,
        exception: e,
        classFqn: e.constructor.name,
        properties: {"user_id": "1"}
    });
}
try {
    // Your code
} catch (Exception exception) {
    var props = new Dictionary<string, string> { { "user_id", "1" } };
    var message = "Error NullPointerException";
    Crashes.LogError(message, props);
}
try {
    // Your code
} catch (Exception exception) {
    var props = new Dictionary<string, string> { { "user_id", "1" } };
    var message = "Error NullPointerException";
    Crashes.LogError(message, props);
}

Details About the Last Crash

Check if the app has previously crashed. This function returns a boolean value.

Crashes.DidCrashInLastSession();

Swift

Crashes.didCrashInLastSession { didCrash in
    // Your code
}

Objective-C

[Crashes didCrashInLastSessionWithCompletion:^(BOOL didCrash) {
    // Your code
}];

Java

Crashes.didCrashInLastSession()

Kotlin

Crashes.didCrashInLastSession()

await AppambitSdk.didCrashInLastSession();
await didCrashInLastSession();
Crashes.DidCrashInLastSession();
Crashes.DidCrashInLastSession();