Best way to programmatically cancel an NSError?

  • I'm wondering how I can best programmatically cancel an NSError. It
    seems I should be looking at willPresentError: but I can't find the
    way. I first tried returning NULL in willPresentError: but that still
    resulted in an empty alert appearing. I did find some mention of
    NSUserCancelledError but I'm not sure if that should be reserved just
    for user canceling. I've looked through the Error Handling Guide but
    I'm missing where this may be addressed.

    Currently, I am overriding presentError:modalForWindow:... in my
    document subclass which seems to work fine, but the docs frown on
    overriding this method. (Not that I'm really messing with it since I
    just call super.):

    - (void)presentError:(NSError *)error modalForWindow:(NSWindow *)
    window delegate:(id)delegate didPresentSelector:(SEL)
    didPresentSelector contextInfo:(void *)contextInfo
    {
    if ([[error domain] isEqualToString:<my apps's unique domain>]==YES) {
      //suppress error
    } else {
      // pass error on up the chain
      [super presentError:error modalForWindow:window delegate:delegate
    didPresentSelector:didPresentSelector contextInfo:contextInfo];
    }
    }

    Just wondering if I'm missing a better way.

    Thanks,
    George
  • So I expect there is a standard way to suppress an NSError... but in
    case I'm doing something too unusual, I'll add some background...

    I have an interesting inter-property validation situation where I
    would like to deny a certain change to an object and instead handle
    it in my controller by creating a new object. (Sounds weird, I know,
    but it does actually make sense in context, I promise!).  Since
    validate<Key>:error: is in the model, I thought it would make sense
    to deny the change and then use the NSError to pass the message to
    the controller layer. My NSDocument subclass can then catch the
    error, suppress it, and react appropriately.

    But how exactly should I suppress the error?

    Thanks,
    George
  • On Fri, 21 Sep 2007 10:58:24 -0400, George Orthwein <george.o...>
    said:
    > So I expect there is a standard way to suppress an NSError... but in
    > case I'm doing something too unusual, I'll add some background...
    >
    > I have an interesting inter-property validation situation where I
    > would like to deny a certain change to an object and instead handle
    > it in my controller by creating a new object. (Sounds weird, I know,
    > but it does actually make sense in context, I promise!).  Since
    > validate<Key> :error: is in the model, I thought it would make sense
    > to deny the change and then use the NSError to pass the message to
    > the controller layer. My NSDocument subclass can then catch the
    > error, suppress it, and react appropriately.
    >
    > But how exactly should I suppress the error?

    I'm not heavily experienced at this, but I've done quite a bit of using
    NSErrors and I'm curious what "suppress the error" means. I'm accustomed to
    doing one of two things:

    * If I catch an NSError arriving thru the responder chain, I can create and
    return a different NSError in willPresentError to substitute it as it heads
    up the chain.

    * If I catch the situation earlier, in validateXXX, I can create an NSError
    and return NO, or just do whatever I like and return YES.

    You seem to be talking about some further option, and I'm wondering what it
    is. Thx - m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
  • On Sep 24, 2007, at 12:30 PM, Matt Neuburg wrote:
    > I'm not heavily experienced at this, but I've done quite a bit of
    > using
    > NSErrors and I'm curious what "suppress the error" means. I'm
    > accustomed to
    > doing one of two things:
    >
    > * If I catch an NSError arriving thru the responder chain, I can
    > create and
    > return a different NSError in willPresentError to substitute it as
    > it heads
    > up the chain.
    >
    > * If I catch the situation earlier, in validateXXX, I can create an
    > NSError
    > and return NO, or just do whatever I like and return YES.
    >
    > You seem to be talking about some further option, and I'm wondering
    > what it
    > is. Thx - m.

    Yes, indeed. In the first case, it seems you can only swap in a
    different error with willPresentError. I'd like to cancel the NSError
    meaning that no alert will get shown to the user.

    In your second example, you can fail to generate the error in
    validateXXX, but can it be "stopped" once it is generated?

    Your message prompted me to further investigate NSUserCancelledError.
    From the Error Handling Guide:

    > Important: You should always special-case test for the
    > NSUserCancelledError error code (in the NSCocoaErrorDomain). This
    > code indicates that the user cancelled the operation (for example,
    > by pressing Command-period). In this case, you should not display
    > any error dialog.

    http://developer.apple.com/documentation/Cocoa/Conceptual/
    ErrorHandlingCocoa/ErrorRespondRecover/chapter_3_section_3.html

    So apparently there is a way to "not display" the error once generated.

    And found in Document-Based Applications Overview:

    > If you override such a method to prevent some action, but you don't
    > want an error alert to be presented to the user, return an error
    > object whose domain is NSCocoaErrorDomain and whose code is
    > NSUserCancelledError. The Application Kit presents errors through
    > the NSApplication implementations of the presentError: and
    > presentError:modalForWindow:delegate:didPresentSelector:contextInfo: m
    > ethods declared by NSResponder. Those implementations silently
    > ignore errors whose domain is NSCocoaErrorDomain and whose code is
    > NSUserCancelledError.

    http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/
    Articles/ErrorHandling.html

    So it seems there IS a standard way to programmatically "cancel"
    errors. I can simply swap in an error with the above specs in
    willPresentError: and it will not get shown to the user.

    Thanks for prompting me to research further!

    George
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today