Reputation: 800
I am working on some API (Crittercism) to report handled exceptions in the client to server.
The client API takesNSException
as the input parameter. I have to add some application context info string(NSString
) also to theNSException
before calling the API.
How I can do it using Objective-C.
NSString* appContextString;
NSString *test = @"test";
unichar a;
int index = 5;
@try {
a = [test characterAtIndex:index];
}
@catch (NSException *exception) {
// add app context to exception before reporting to Crittercism.
[Crittercism logHandledException:exception];
}
I have to append appContextString to exception.
Upvotes: 1
Views: 1372
Reputation: 16660
You have to copy it. This can be done easily. An instance of NSExeption
has three important properties:
When copying it, you can add a string to the user info dictionary:
NSMutableDictionary *userInfo = [exception.userInfo mutableCopy];
userInfo[@"YourPrivateKey"] = contextString;
NSException *extendedException = [NSException exceptionWithName:exception.name reason:exception.reason userInfo:userInfo];
I think as a caveat that you will lose the call stack. You can read that and put it in the user info dictionary, too. I do it a similar way on Objective-Cloud.
userInfo[@"CallStackSymbols2] = exception.callStackSymbols;
Analogous with call stack return addresses.
I do not think that this is very nice, because the call stack becomes a part of the exceptions's user info instead of the exception itself. For me this is no caveat, because I sent the exception as JSON, therefore the call stack as JSON object. I cannot say, whether this is a problem for you.
Upvotes: 1
Reputation: 122391
You could build a new NSException
object from attributes of the old one, but that seems very messy. As you are just logging the handled exception, I would simply log the "app context" before that:
@try {
a = [test characterAtIndex:index];
}
@catch (NSException *exception) {
NSString *appContext = @"...";
[Crittercism leaveBreadcrumb:appContext];
[Crittercism logHandledException:exception];
}
Upvotes: 1