NSXMLParserDelegateAbortedParseError
NSXMLParserDelegateAbortedParseError
- Subject: NSXMLParserDelegateAbortedParseError
- From: Josh Caswell <email@hidden>
- Date: Mon, 07 Mar 2011 19:05:31 -0800
This is a copy-paste of a question I asked on StackOverflow <
http://stackoverflow.com/questions/5132266/nsxmlparser-error-code-changes-after-abort>,
which hasn't gotten any answers. I'm still curious about it and hoped I
might get a response on this list; apologies to anyone who is reading it
twice now.
I'm using an NSXMLParser on some data that I request from a server on the
net. It's possible that one of the arguments to the request is not to the
server's liking, and it will return some XML with an Error element. In that
case, I present the server's error text in an alert sheet, so the user has
some idea what may have gone wrong. My parser delegate facilitates this by
saving the contents of the ErrorMessage element that the parser finds, and
then calling [parser abortParsing]
The error code that results from this is supposed to be code 512,
NSXMLParserDelegateAbortedParseError, and, indeed, when my delegate
implements:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError
*)parseError {
NSLog(@"Parse Error Code: %d", [parseError code]);
}
I see 512 in the log. So far, so good.
Now, my parser delegate returns the result of [parser parse] to my
controller. The controller checks that result, and if it's NO, it checks the
code in the error pointer that it passed, to see if failure was due to the
delegate aborting:
// In MyParserDelegate.m
- (BOOL)parseData:(NSData *)data
intoDict:(NSMutableDictionary *)dict
errPtr:(NSError **)errPtr {
// ...
BOOL successful = [xmlParser parse];
if( !successful && (errPtr != NULL) ){
*errPtr = [parser parserError];
NSLog(@"Delegate: Parser Error Code: %d", [*errPtr code]);
// Should be 512 after abort? Is actually 1...
}
return successful;
}
// In MyController.m
BOOL successful = [parserDelegate parseData:data
intoDict:dict
errPtr:&error];
if( !successful ){
NSLog(@"Controller: Parse Error Code: %d", [error code]);
// Should be 512 after abort? Is actually 1...
}
I expect this to be the same code 512, but it turns out that it is code 1,
NSXMLParserInternalError! I'd like to figure out what's going on here so I
don't end up trying to present an error in the wrong circumstances.
The obvious ways around this are to either post a notification or implement
a flag (which I did) in the delegate so it knows when it aborted, and can
construct its own NSError to return in place of the actual parserError.
I'm just trying to understand why the error code changes like this. Is the
delegate supposed to do something in parseErrorOccurred: to tell the parser
that it should hold onto the AbortedParseError? (The only Sample Code that
checks for that error also ignores it.) Also, not knowing the circumstances
that would otherwise cause an NSXMLParserInternalError (Apple's description
of it is so wonderfully eloquent), I would like to avoid catching it when I
didn't mean to. Can anyone enlighten me, particularly on the question of the
code change?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden