Raising an exception on a message hook doesn't fail the Java

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
pedrofmj
Posts: 20
Joined: Sun Jan 18, 2015 1:15 pm

Raising an exception on a message hook doesn't fail the Java

Post by pedrofmj » Wed Jan 21, 2015 6:11 am

Hi Dr. Paulo,

I'm trying to forward an exception from a Logtalk unit test to a JUnit Test (Logtalk on Java through JPL/SWI-Prolog)

The exception is correctly generated from Logtalk:

[exec] [junit] ERROR: /home/pedrofmj/logtalk/examples/ack/.lgt_tmp/tester_lgt.pl:1: Initialization goal raised exception:
[exec] [junit] ERROR: Java exception: com.egypteam.ai.khalid.util.JUnitUtil$JUnitException: failure_instead_of_success

It was generated through this code:

logtalk::message_hook(failed_test(Test, File, Position, Reason), _, lgtunit, _) :-
jpl_new('com.egypteam.ai.khalid.util.JUnitUtil', [], U),
jpl_call(U, throwTestException, [Reason], _).

The problem is that the exception is not forwarded to the Java Unit Test (see it at the bottom of the message)
For how it was designed, it should have printed "PASSOU AQUI" and failed the JUnit Test.
But the problem is that the Query object isn't receiving the exception as an exception, but as a ERROR message only.
Please, is this approach valid?
If not, do you see another approach to do this?

Thanks in advance,

Pedro Ferreira.

Java Unit Test Code:
@Test
public void testLoad1() throws IOException {

try {

String exampleName = "ack";

ltLoader.loadExample(exampleName);


String exampleDirectory = System.getenv("LOGTALKUSER") + "/examples/" + exampleName;

//Query q1 = new Query("time(ack::ack(2,4,V))");
//Query q1 = new Query("ack::ack(2,4,V)");
Query q1 = new Query("logtalk_load('" + exampleDirectory + "/tester.lgt')");

if (false) {
Hashtable[] hts = q1.allSolutions();
for (int i = 0; i < hts.length; i++) {
//System.out.println(hts.toString());
}
} else {
q1.open();
int solutionNumber = 0;
while (q1.hasMoreSolutions()) {
Hashtable ht = q1.nextSolution();
//System.out.println("Solution " + (++solutionNumber) + ": " + ht.toString());
}
q1.close();
}
} catch (Exception e) {

System.out.println("PASSOU AQUI");
fail(e.getMessage());

}


}

pedrofmj
Posts: 20
Joined: Sun Jan 18, 2015 1:15 pm

Re: Raising an exception on a message hook doesn't fail the

Post by pedrofmj » Wed Jan 21, 2015 11:58 am

Hi Dr. Paulo,

I tryied a lot of ways to do the exception be catched by the JUnit test, and it failed.

It simply doens't forward the exception and put the message as an ERROR from logtalk message.

I need turn the ERROR message and actual JUnit failing.

[exec] [junit] ERROR: /home/pedrofmj/logtalk/examples/ack/.lgt_tmp/tester_lgt.pl:1: Initialization goal raised exception:
[exec] [junit] ERROR: Java exception: com.egypteam.ai.khalid.util.JUnitUtil$JUnitException: failure_instead_of_success <--- The excpetion is generated, but is converted in Logtalk output.


Please, I can provide a gist to you look at if possible.

I need help on this.

Any ideas?

Thank you,

Pedro.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Raising an exception on a message hook doesn't fail the

Post by Paulo Moura » Wed Jan 21, 2015 4:14 pm

From the JPL documentation on exceptions, it seems clear that the problem is in your Java code, which is not handling/catching the exception:

http://www.swi-prolog.org/packages/jpl/ ... exceptions
Paulo Moura
Logtalk developer

pedrofmj
Posts: 20
Joined: Sun Jan 18, 2015 1:15 pm

Re: Raising an exception on a message hook doesn't fail the

Post by pedrofmj » Wed Jan 21, 2015 6:47 pm

Hi Dr. Paulo.

In fact, in theory, There is no problem in the Exception catching...

It's because I'm trying to do a generic bridge Prolog Exception <-> Java Exception for the unit tests. But I'm now thinking this is not possible with the current unit test code.
the exceptions are converted into ERROR MESSAGES. They can't be catched. Please, correct me if I'm wrong.

All I can do for now is specific tests for each case, testing the variables and queries results.

Thanks,

Pedro.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Raising an exception on a message hook doesn't fail the

Post by Paulo Moura » Wed Jan 21, 2015 7:45 pm

You're wrong. Re-read the JPL documentation on exceptions. The first sentence explains the output you're seeing: Uncaught exceptions thrown by the JVM in the course of handling a JPL 3.x Prolog API call are mapped onto Standard Prolog exceptions (...). You get the the following output:

Code: Select all

ERROR: Java exception: com.egypteam.ai.khalid.util.JUnitUtil$JUnitException: failure_instead_of_success
printed by Prolog because the throwTestException(Reason) message results in a Java exception that is not handled. Again, straight from the documentation: Java exceptions are always returned as Prolog exceptions with this structure: java_exception( classname, reference_to_exception_object). Btw, you if look into the JPL Prolog code in the "jpl.pl" file, around line 4371, you'll find (part of) the code that generates the output you get:

Code: Select all

:- multifile
	prolog:error_message/3.

prolog:error_message(java_exception(Ex)) -->
	(   { jpl_call(Ex, toString, [], Msg)
	    }
	->  [ 'Java exception: ~w'-[Msg] ]
	;   [ 'Java exception: ~w'-[Ex] ]
	).
Paulo Moura
Logtalk developer

pedrofmj
Posts: 20
Joined: Sun Jan 18, 2015 1:15 pm

Re: Raising an exception on a message hook doesn't fail the

Post by pedrofmj » Wed Jan 21, 2015 8:02 pm

Ok. I'll try that. Thank you,

Pedro.

pedrofmj
Posts: 20
Joined: Sun Jan 18, 2015 1:15 pm

Re: Raising an exception on a message hook doesn't fail the

Post by pedrofmj » Wed Jan 21, 2015 8:48 pm

Hi Dr. Paulo,

Thanks for you help to me back being a mindcracker.

What worked:

logtalk::message_hook(failed_test(Test, File, Position, Reason), _, lgtunit, _) :-
halt.

Only this small change solved the entire problem!

Maybe missed some context:

I want to HALT the build process when we get any error on Unit tests, whatever Java or Prolog.

Thanks very much,

Pedro Ferreira.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest