• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Aggregating ValidationExceptions in validateForSave()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Aggregating ValidationExceptions in validateForSave()


  • Subject: Re: Aggregating ValidationExceptions in validateForSave()
  • From: Ramsey Gurley <email@hidden>
  • Date: Wed, 14 Jan 2015 12:24:13 -0700

I’m pretty sure Chuck is always right™. Even when he answers a question 6 years before the code in question was released. If it’s between Chuck and Javadocs, my money is on Chuck ;-D

On Jan 14, 2015, at 11:23 AM, Chuck Hill <email@hidden> wrote:

IIRC, it should work like the first example, but it is possible that some code is creating the thrown exception incorrectly.  The last code example is probably safe.

Chuck

On 2015-01-13, 9:36 PM, "Paul Hoadley" wrote:

Hello,

Just digging up a 12 year old thread on a different list here...

http://webobjects-dev.omnigroup.narkive.com/qCLbaSnX/throwing-multiple-validation-errors-during-validateforsave

Chuck suggests something like the following:

public void validateForSave() throws ValidationException {
NSMutableArray exceptions = new NSMutableArray();
try {
super.validateForSave();
} catch (ValidationException e) {
exceptions.addObjectsFromArray(e.additionalExceptions());
}

// ...

if (exceptions.count() > 0) {
throw ValidationException.aggregateExceptionWithExceptions(exceptions);
}
}

Chuck, does that miss the case where super.validateForSave() just throws a single exception e without nested exceptions?  In that case, e.additionalExceptions() will return an empty array, and e doesn't get added to the exceptions array.  That seems to be what's happening for me, anyway.

I don't think this would be quite right:

try {
super.validateForSave();
} catch (ValidationException e) {
exceptions.add(e);
exceptions.addObjectsFromArray(e.additionalExceptions());
}

because the Javadocs suggest the container exception duplicates the first exception in the array.

http://wocommunity.org/documents/javadoc/WebObjects/5.4.2/com/webobjects/foundation/NSValidation.ValidationException.html#aggregateExceptionWithExceptions(com.webobjects.foundation.NSArray)

How about this?

try {
super.validateForSave();
} catch (ValidationException e) {
if (e.additionalExceptions().count() > 0) {
exceptions.addObjectsFromArray(e.additionalExceptions());
} else {
exceptions.add(e);
}
}

I'll test it out, but if someone already knows, by all means jump in.


-- 
Paul Hoadley
http://logicsquad.net/


Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Aggregating ValidationExceptions in validateForSave() (From: Paul Hoadley <email@hidden>)
 >Re: Aggregating ValidationExceptions in validateForSave() (From: Chuck Hill <email@hidden>)

  • Prev by Date: Re: Aggregating ValidationExceptions in validateForSave()
  • Next by Date: Re: WOLips for Eclipse 4.4 binary
  • Previous by thread: Re: Aggregating ValidationExceptions in validateForSave()
  • Next by thread: Websphere/other Appserver Deployment with two EARs/WARs in one Node?
  • Index(es):
    • Date
    • Thread