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: Jerry Krinock <email@hidden>
- Date: Wed, 3 Mar 2010 21:41:28 -0800
On 2010 Mar 03, at 13:52, Keith Blount wrote:
> So, here is my error-setting:
<code snipped out>
Assuming this code is in your -readFromURL:ofType:error: implementation, it looks OK to me, except that I'd suggest you give that error a nonempty domain and a nonzero code. For the former, [[NSBundle mainBundle] bundleIdentifier] is a quick hack. But I have no reason to believe that this will work any better.
> That works okay, except that the failure reason doesn’t get displayed.
Well Keith, what do you expect from a free implementation :)) I agree; the NSError presentation built into Cocoa is too way too wimpy. Failure to display the failure reason is probably one of the many reasons why I wrote my own.
> The documentation clearly states that you can tell NSDocument not to present an error by setting its domain to NSCocoaErrorDomain and its code to NSUserCancelledError:
Yes, that is true.
> However, this simply doesn’t work for NSDocument’s -readFromURL:ofType:error: I have tried to set outError as follows:
>
> if (outError)
> *outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil];
I'm not sure where you've put that code, but it's probably wrong. Here is what works for me. I subclass NSDocumentController and put the following override in that subclass.
- (NSError *)willPresentError:(NSError*)error {
[SSYAlert alertError:error] ;
return [NSError errorWithDomain:NSCocoaErrorDomain
code:NSUserCancelledError
userInfo:nil] ;
}
In the above, SSYAlert is my alert class. In this case, since the document failed to be created, there is no window to attach a sheet to, so it produces a dialog.
I suppose the idea is that, even though, according to "Message Flow in the Document Architecture", "The NSDocument object reads the contents of the file by sending the readFromURL:ofType:error: message to itself", the failed NSDocument instance fails to initialize and returns the error down the call stack to -[NSDocumentController initWithType:error:].
> I could put all of the information into NSLocalizedDescriptionKey, but this looks horrible as it’s all in bold
Ah, yes. That's another reason why I don't use Cocoa's error presentation. I hate that boldface crap.
_______________________________________________
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