You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by "Alexander Lamb (dev)" <al...@mac.com> on 2008/02/26 10:45:07 UTC

Raising an exception in a callback

Hello List,

I want to prevent a delete of an object if certain attributes have  
certain values (e.g. do not delete an invoice which has already been  
sent).

I registered the callback in the Modeler. Works fine.

I do my test in the callback function I defined, it works fine.

Except that to "stop" the delete, I raise a RuntimeException (I don't  
see any other way). It works and is catched by the delete. However I  
do not get my exception, but an exception Cayenne has generated.

What is the correct way to raise an exception and transfer up the  
correct error message?

Thanks,

Alex


Re: Raising an exception in a callback

Posted by "Alexander Lamb (dev)" <al...@mac.com>.
Works fine, thanks! (my lack of Java knowledge....)


Le 26 févr. 08 à 14:08, Andrus Adamchik a écrit :

>
> On Feb 26, 2008, at 3:04 PM, Alexander Lamb (dev) wrote:
>
>>
>> e.getCause() will give me an InvocationTargetException.
>
> Sorry, I should've been more explicit. You need to unwrap the  
> exception all the way to the root cause:
>
> Throwable th = e;
> while(th.getCause() != null) {
>   th = th.getCause();
> }
>
>
> Andrus


Re: Raising an exception in a callback

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Feb 26, 2008, at 3:04 PM, Alexander Lamb (dev) wrote:

>
> e.getCause() will give me an InvocationTargetException.

Sorry, I should've been more explicit. You need to unwrap the  
exception all the way to the root cause:

Throwable th = e;
while(th.getCause() != null) {
    th = th.getCause();
}


Andrus

Re: Raising an exception in a callback

Posted by "Alexander Lamb (dev)" <al...@mac.com>.
I can't seem to get the original exception.

In my Object:

	public void checkBeforeDelete() {
		if(getInvoiced() != null && getInvoiced())
			throw new RuntimeException("*** Invoice created " +  
getFormattedCreationDate() + " is invoiced, can't delete");
	}


Then I catch the error the following way:

		try {
			_session.getDataContext().deleteObject(invoice);
			_session.getDataContext().commitChanges();
		} catch(Exception e) {
			System.out.println("***** CAN'T DELETE INVOICE: " + e.getMessage());
			_session.getDataContext().rollbackChanges();
		}


e.getCause() will give me an InvocationTargetException.

Alex

Le 26 févr. 08 à 10:56, Andrus Adamchik a écrit :

>
> On Feb 26, 2008, at 11:45 AM, Alexander Lamb (dev) wrote:
>
>> Hello List,
>>
>> I want to prevent a delete of an object if certain attributes have  
>> certain values (e.g. do not delete an invoice which has already  
>> been sent).
>>
>> I registered the callback in the Modeler. Works fine.
>>
>> I do my test in the callback function I defined, it works fine.
>>
>> Except that to "stop" the delete, I raise a RuntimeException (I  
>> don't see any other way). It works and is catched by the delete.  
>> However I do not get my exception, but an exception Cayenne has  
>> generated.
>>
>> What is the correct way to raise an exception and transfer up the  
>> correct error message?
>>
>> Thanks,
>>
>> Alex
>
> Without looking at the code, IIRC CayenneRuntimeException.getCause()  
> should store your exception.
>
> Andrus


Re: Raising an exception in a callback

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Feb 26, 2008, at 11:45 AM, Alexander Lamb (dev) wrote:

> Hello List,
>
> I want to prevent a delete of an object if certain attributes have  
> certain values (e.g. do not delete an invoice which has already been  
> sent).
>
> I registered the callback in the Modeler. Works fine.
>
> I do my test in the callback function I defined, it works fine.
>
> Except that to "stop" the delete, I raise a RuntimeException (I  
> don't see any other way). It works and is catched by the delete.  
> However I do not get my exception, but an exception Cayenne has  
> generated.
>
> What is the correct way to raise an exception and transfer up the  
> correct error message?
>
> Thanks,
>
> Alex

Without looking at the code, IIRC CayenneRuntimeException.getCause()  
should store your exception.

Andrus