Hi All,
I am currently writing unit test cases where exception mappers are being
used in my service. Intermittently I get the following error when running
my tests :
==============
java.lang.ClassCastException:
org.wso2.carbon.andes.service.exceptions.InternalServerException cannot be
cast to org.wso2.carbon.andes.service.exceptions.MessageNotFoundException
at
org.wso2.carbon.andes.service.internal.exception.mappers.MessageNotFoundMapper.toResponse(MessageNotFoundMapper.java:30)
at
org.wso2.msf4j.internal.MSF4JMessageProcessor.handleThrowable(MSF4JMessageProcessor.java:138)
at
org.wso2.msf4j.internal.MSF4JMessageProcessor.receive(MSF4JMessageProcessor.java:81)
at
org.wso2.carbon.transport.http.netty.listener.WorkerPoolDispatchingSourceHandler$1.run(WorkerPoolDispatchingSourceHandler.java:125)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
==============
In the above exception, "MessageNotFoundException" maps to
"MessageNotFoundMapper" and "InternalServerException" maps to
"InternalServerErrorMapper".
The 2 exception classes have nothing in common and the 2 mapper classes
also have nothing in common.
I debugged further to find the issue with the attached
patch(ex-map-msf4j.diff) for logging(not sure whether I have the latest
code).
Following is the output when the error occurs :
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.DestinationNotFoundMapper.toResponse(
*java.lang.Throwable*)
....
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.InternalServerErrorMapper.toResponse(
*java.lang.Throwable*)
....
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.MessageNotFoundMapper.toResponse(
*java.lang.Throwable*)
The issue here is that the paramtype is as "java.lang.Throwable". The
correct value should be as follows since there is a comparator for the
exceptionMapper map :
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.DestinationNotFoundMapper.toResponse(
*org.wso2.carbon.andes.service.exceptions.DestinationNotFoundException*)
.....
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.InternalServerErrorMapper.toResponse(
*org.wso2.carbon.andes.service.exceptions.InternalServerException*)
.....
[org.wso2.msf4j.internal.MicroservicesRegistry] : CLASS : public
javax.ws.rs.core.Response
org.wso2.carbon.andes.service.internal.exception.mappers.MessageNotFoundMapper.toResponse(
*org.wso2.carbon.andes.service.exceptions.MessageNotFoundException*)
According to what I was able to find out, this is an JDK error[1][2](Bug:
8029459 <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8029459>) for
the method "em.getClass().getMethods()"[3].
Any advice on how to proceed or that needs to be checked from my side ?
[1] -
http://www.oracle.com/technetwork/java/javase/8-known-issues-2157115.html
[2] - http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8029459
[3] -
https://github.com/wso2/msf4j/blob/master/core/src/main/java/org/wso2/msf4j/internal/MicroservicesRegistry.java#L84
Regards,
Hemika
Hemika Kodikara
Software Engineer
WSO2 Inc.
lean . enterprise . middleware
http://wso2.com
Mobile : +94777688882
diff --git
a/core/src/main/java/org/wso2/msf4j/internal/MicroservicesRegistry.java
b/core/src/main/java/org/wso2/msf4j/internal/MicroservicesRegistry.java
index 075b4e7..44e35e2 100644
--- a/core/src/main/java/org/wso2/msf4j/internal/MicroservicesRegistry.java
+++ b/core/src/main/java/org/wso2/msf4j/internal/MicroservicesRegistry.java
@@ -86,7 +86,9 @@ public class MicroservicesRegistry {
findAny().
ifPresent(method -> {
try {
+ log.info("CLASS : " + method.toString());
exceptionMappers.put(Class.forName(method.getGenericParameterTypes()[0].getTypeName()),
em);
+ log.info("MAPPING ADDER : " + exceptionMappers);
} catch (ClassNotFoundException e) {
log.error("Could not load class", e);
}
@@ -95,6 +97,7 @@ public class MicroservicesRegistry {
}
Optional<ExceptionMapper> getExceptionMapper(Throwable throwable) {
+ log.info("EXCEPTION MAPPERS : " + exceptionMappers);
return exceptionMappers.entrySet().
stream().
filter(entry ->
entry.getKey().isAssignableFrom(throwable.getClass())).
@@ -110,6 +113,7 @@ public class MicroservicesRegistry {
ifPresent(method -> {
try {
exceptionMappers.remove(Class.forName(method.getGenericParameterTypes()[0].getTypeName()));
+ log.info("MAPPING REMOVING : " + exceptionMappers);
} catch (ClassNotFoundException e) {
log.error("Could not load class", e);
}
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev