Search code examples
macoscocoaerror-handlingnserrornsalert

Which fields does -[NSResponder presentError:] show?


Is there any documentation about how the -[NSResponder presentError:] method works? I'm trying to create NSError instances in my library whose userInfo values show as much useful information to the user as possible. I'm having a tough time.

I started out using just NSLocalizedFailureReasonErrorKey. Seeing that it assigned the messageText property of the generated NSAlert (bold font, topmost label), I next tried adding on NSLocalizedDescriptionKey, which I thought would then fill in the informativeText property, for a non-bold explanation under it. I also added NSURLErrorKey, since this library deals with files and it would be helpful.

The resulting NSAlert doesn't show the original NSLocalizedFailureReasonErrorKey value at all, with the new NSLocalizedDescriptionKey now becoming the messageText. It also doesn't expose the NSURLErrorKey at all, either.

Is my only option to write my own error presentation method and special-case this library's domain?


Solution

  • How the NSError keys interact with presentError: is documented in the Error Handling Programming Guide under Localized Error Information.

    It includes a diagram detailing what goes where. As the docs note:

    Failure reason

    A brief sentence that explains the reason why the error occurred. It is typically part of the error description. Methods such as presentError: do not automatically display the failure reason because it is already included in the error description. The failure reason is for clients that only want to display the reason for the failure.

    If you want to rearrange the fields in an NSError object prior to presentation, this is best done by overriding willPresentError: to return an adjusted error object.

    Regarding fields like NSURLErrorKey, it is expected that you will have included this in NSLocalizedDescriptionKey or NSLocalizedRecoverySuggestionErrorKey if you want to display it. While it is called the "recovery suggestion," the documentation is explicit that it may be used "as a purely informative message supplementing the error description and failure reason."