I have JSON-RPC Service (a class with methods invoked using Reflection) and I want to pass exception as data to error object. So I try to catch Exception in Servlet but e.getMessage() return null, showing backtrace don't show my code at all (only function that get Stack trace and a place where I call it):
public Object loadService() throws InstantiationException, ClassNotFoundException, IllegalAccessException {
ClassLoader parentClassLoader = ServiceReloader.class.getClassLoader();
ServiceReloader classLoader = new ServiceReloader(parentClassLoader);
//ClassLoader classLoader = init.class.getClassLoader();
Class aClass = classLoader.loadClass("pl.jcubic.Service");
return aClass.newInstance();
}
public String backTrace() {
StackTraceElement[] stack = (new Throwable()).getStackTrace();
String trace = "";
for (int i=0; i<stack.length; ++i) {
trace += stack[i].toString() + "\n";
}
return trace;
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object id = null;
try {
JSONRPC2Request reqIn = JSONRPC2Request.parse(this.getInputData(request));
id = reqIn.getID();
Object service = loadService();
Method[] methods = service.getClass().getMethods();
Object result = method.invoke(service, params);
JSONRPC2Response respOut = new JSONRPC2Response(result, id);
out.println(respOut);
} catch (InvocationTargetException e) {
out.println(internalError(id, e.getMessage(), backTrace()));
}
}
when a method throw Exception like throw new Exception("Error occured");
(that message I want to send using JSON-RPC) I got this backtrace,
init.stackTrace(init.java:65)
init.doPost(init.java:119)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:724)
Is there any way to display my Exception I throw?
I have two errors here:
I use new Trowable to create backtrace I should use existing exception:
And I need to use getCause() method of Throwable to get real Exception (don't know if I only need to use it because of reflection):