Re: Returning a useful error from NSDocument's -readFromURL:...
Re: Returning a useful error from NSDocument's -readFromURL:...
- Subject: Re: Returning a useful error from NSDocument's -readFromURL:...
- From: Keith Blount <email@hidden>
- Date: Thu, 4 Mar 2010 05:21:46 -0800 (PST)
Many thanks both for your replies.
As it happens, I need to slap my own forehead for stupidity. I mentioned in my e-mail that returning an error with NSLocalizedDescriptionKey and NSLocalizedRecoverySuggestionErrorKey "used to work", and then foolishly assumed it had been an update in Snow Leopard that must have changed something. But of course, I was just being an idiot. After reading your replies and hearing that what I was doing *should* work, I created a dummy project and tried doing the same there (sorry, I really should have done that before posting). And of course, it worked fine. So something in my rather large project has obviously broken the way errors are displayed.
As I say, I have two problems:
First, if I do this in -readFromURL:ofType:error:
if (outError)
{
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:NSLocalizedString(@"Invalid XML Data", @"") forKey:NSLocalizedDescriptionKey];
[userInfo setObject:NSLocalizedString(@"The document cannot be loaded because the XML file contained invalid characters.",nil) forKey: NSLocalizedRecoverySuggestionErrorKey];
*outError = [NSError errorWithDomain:@"" code:0 userInfo:userInfo];
}
only the NSLocalizedDescriptionKey gets displayed in the resulting error panel - the recovery suggestion is ignored (even if I enter a domain and non-zero code).
Secondly, if I do this:
if (outError)
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil];
to cancel the error, I still get a panel saying nothing more than "Alert".
Well, the second problem was easy enough - it was down to a coding mistake on my part. My NSApp delegate implements -application:openFile: and after its call to NSDocumentController's -openDocumentWithContentsOfURL:display:error:, instead of calling NSDocumentController's -presentError: message, I was calling [[NSAlert alertWithError:error] runModal]. So obviously the cancelled error was getting put up here, duh.
So that just leaves me to figure out how on earth I have screwed things up so that the default error handling doesn't show NSLocalizedRecoverySuggestionErrorKey. Clearly something in my code is preventing this from happening, although I'm not sure what now. I tried commenting out -application:openFile: altogether to allow the file to be opened without any of my custom handling procedures, but the error still didn't show the recovery suggestion.
Does anyone have any ideas about how you can mess things up so that the recovery error wouldn't be shown?
Many thanks and all the best,
Keith
_______________________________________________
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