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

[[MParticle sharedInstance] logError:@"Login failed"
                           eventInfo:@{@"reason":@"Invalid username"}];

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 {      
    [self callNonExistingMethod];
}
@catch (NSException *ex) {
    [[MParticle sharedInstance] logException:ex];
}

// An exception reporting the topmost context at the moment of the exception
@try {      
    dictionary[@"key"] = nil;
}
@catch (NSException *ex) {        
    [[MParticle sharedInstance] logException:ex topmostContext:self];
}

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.

// Begins handling uncaught exceptions
[[MParticle sharedInstance] beginUncaughtExceptionLogging];

// Ends handling uncaught exceptions
[[MParticle sharedInstance] endUncaughtExceptionLogging];

In addition to enabling and disabling unhandled exceptions programmatically, you can also do it server side, in the console, without any need for client side code change.

Unhandled Exceptions are not currently supported for tvOS.

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.

// Leaving breadcrumbs along the way of parsing an object that could throw an exception
- (void)parseResource:(Resource *)resource {
    MParticle *mParticle = [MParticle sharedInstance];

    @try {
        [mParticle leaveBreadcrumb:@"parsing began"];

        [mParticle leaveBreadcrumb:@"parsing title"];
        [resource parseTitle];

        [mParticle leaveBreadcrumb:@"parsing body"];
        [resource parseBody];

        [mParticle leaveBreadcrumb:@"parsing footer"];
        [resource parseFooter];

        [mParticle leaveBreadcrumb:@"parsing finished!"];
    }
    @catch (NSException *ex) {
        [mParticle logException:ex topmostContext:self];
    }
}