Error Tracking

  • An ‘error’ is a designed error state in your app, such as a failed login.
  • An ‘exception’ is an error thrown by the app or site.

Additional attributes can be sent with a logError or logException command.

Log Errors

Map<String, String> errorInfo = new HashMap<String, String>();
errorInfo.put("reason", "Invalid username");

MParticle.getInstance().logError("Login failed", errorInfo);
HashMap<String, String>().apply {
    put("reason", "Invalid username")
}.also {
    MParticle.getInstance().logError("Login failed", it)
}

Log Exceptions

Exceptions are signaling mechanisms used by languages and APIs to indicate that an unexpected or impossible action was attempted. If an exception is wrapped within a try/catch block, the app will be able to recover, and you can log that exception using the mParticle SDK.

Any available state information, as well as a stack trace at the moment of the exception, is sent together with the logException command.

try {
    someDangerousMethod();
} catch (NullPointerException npe) {
    MParticle.getInstance().logException(npe);
}
try {
    someDangerousMethod();
} catch (npe: NullPointerException) {
    MParticle.getInstance().logException(npe);
}

Automatic Exception Tracking

If you wish to listen for unhandled exceptions with the mParticle SDK, not that only one listener can be created, so if you have other SDKs listening for unhandled exceptions, such as HockeyApp or Fabric, these may conflict.

Enable this feature in the Android SDK via XML configuration:

<bool name="mp_reportUncaughtExceptions">true</bool>

You can also enable it programmatically:

//enable
MParticle.getInstance().enableUncaughtExceptionLogging();

//disable
MParticle.getInstance().disableUncaughtExceptionLogging();

While debugging a scenario that may lead or is currently leading to crashes and/or exceptions, it is often helpful to leave breadcrumbs along the way to better understand the context leading to the problem. Things like which interactions a user had with the app, or to identify step-by-step what led to a particular line of code being executed.

The mParticle SDK lets you leave breadcrumbs with the leaveBreadcrumb method. You can also include additional custom attributes.

//fictional method that parses some object
private void parseResource(Resource someResource) {
    MParticle mp = MParticle.getInstance();

    try {
        mp.leaveBreadcrumb("parsing began");

        mp.leaveBreadcrumb("parsing title");
        parseResourceTitle(someResource);

        mp.leaveBreadcrumb("parsing body");
        parseResourceBody(someResource);

        mp.leaveBreadcrumb("parsing footer");
        parseResourceFooter(someResource);

        mp.leaveBreadcrumb("parsing finished!");
    } catch (Exception ex) {
        mp.logException(ex);
    }
}
//fictional method that parses some object
private fun parseResource(someResource: Resource) {
    MParticle.getInstance().apply {
        try {
            leaveBreadcrumb("parsing began")

            leaveBreadcrumb("parsing title")
            parseResourceTitle(someResource)

            leaveBreadcrumb("parsing body")
            parseResourceBody(someResource)

            leaveBreadcrumb("parsing footer")
            parseResourceFooter(someResource)

            leaveBreadcrumb("parsing finished!")
        } catch (ex: Exception) {
            logException(ex)
        }
    }
}