Our application has unexpected events which are exceptions that are not caught. So when one of these happens, you get into the method public void onError( RESTException e ) in the RESTCallback. The specific error is the unparsable json error, which I suppose makes sense if an exception is being returned. So I decided to trap all exceptions with the following filter class:
package com.impulse.mc.server;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletResponse;
public class ExceptionFilter implements Filter
{
@Override
public void destroy()
{
// TODO Auto-generated method stub
}
@Override
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
{
try
{
chain.doFilter( request, response );
} catch ( Throwable t )
{
// SC_INTERNAL_SERVER_ERROR
HttpServletResponse httpResponse = (HttpServletResponse) response;
String msg = generateFault( "Sender", "Beta", "Gamma" ) ;
httpResponse.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
// httpResponse.setStatus( HttpServletResponse.SC_SEE_OTHER, "A generic error" );
//httpResponse.getOutputStream().print( msg );
// httpResponse.getWriter().print( msg );
}
}
@Override
public void init( FilterConfig arg0 ) throws ServletException
{
// TODO Auto-generated method stub
}
private String generateFault( String code, String subcode, String reason )
{
StringBuffer buff = new StringBuffer();
buff.append( "{\"Fault\":{\"Code\":{\"Value\":\"" ).append( code ).append( "\",\"Subcode\":{\"Value\":\"" ).append( subcode ).append( "\"}},\"Reason\":{\"Text\":\"" ).append( reason ).append( "\"}}}" );
return buff.toString();
}
// private String generateFault( String code, String subcode, String reason )
// {
// StringBuffer buff = new StringBuffer();
//
// buff.append( "{\"Fault\":{\"Code\":{\"Value\":\"" ).append( code ).append( "\",\"Subcode\":{\"Value\":\"" ).append( subcode ).append( "\"}},\"Reason\":{\"Text\":\"" ).append( reason ).append( "\"},\"Detail\":{\"MaxTime\":\"PSM\"}}}" );
// return buff.toString();
// }
}
Anyway, I do hit the breakpoint in the catch ( Throwable t ) area. At this point I want to craft a response with useful information. As you can see, I have tried several methods of doing this. The end result is that I do end up in the RestException block, but always with the unparsable json error. No other data comes through other than the server error code number. All the documentation I read says the above should work. I know the generateFault() method does return valid json. I found 2 examples of this, hence the two methods.
Does anyone know what I am doing wrong? Essentially I want to create a global trap for server exceptions, and return some additional information with the error. In my example all this is fixed information, but I can't even get that to appear on the client side. I suspect I am making a simple mistake on how to return this valid json string. Or maybe this methodology is totally wrong? I do not want to put a bunch of try-catch statements in a bunch of methods in dozens of classes.