I am trying to return an error on RESTility.callREST( call, but I can't get anything but a basic error code returned.

23 views
Skip to first unread message

Lord Silverhammer

unread,
Oct 1, 2013, 3:59:49 PM10/1/13
to spif...@googlegroups.com
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.

Zack Grossbart

unread,
Oct 1, 2013, 5:45:06 PM10/1/13
to spif...@googlegroups.com
Hello,

It sounds like you're on the right track. The JSON format for RESTException
is on the bottom of this page:

http://www.spiffyui.org/?rest

However, this specific error indicates that the JSON you're returning to the
client is invalid. I would use a tool like Firebug to get the JSON response
from the server and then copy and paste the JSON into JSONLint here:

http://jsonlint.com/

It will show you what's wrong with the JSON formatting.

Good luck,
Zack
--
You received this message because you are subscribed to the Google Groups
"Spiffy UI" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to spiffy-ui+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

TheSilverHammer

unread,
Oct 1, 2013, 5:47:58 PM10/1/13
to spif...@googlegroups.com
Yes, my JSON is correct. I thought I said it was.  Yet I still get that error.  My guess is that I am not returning the JSON correctly or that there is extra data in the stream corrupting it.  You can run the function yourself, the generateFault() and verify for yourself that the json is correct.


You received this message because you are subscribed to a topic in the Google Groups "Spiffy UI" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spiffy-ui/vzD1jwHysac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spiffy-ui+...@googlegroups.com.

Zack Grossbart

unread,
Oct 1, 2013, 6:12:40 PM10/1/13
to spif...@googlegroups.com
You should make sure to check the response in Firebug. That will show you
the whole response with any extra data which might be corrupting it.

-Zack

TheSilverHammer

unread,
Oct 1, 2013, 6:13:47 PM10/1/13
to spif...@googlegroups.com
Ok I will do.  However assuming it is corrupted somehow, do you have any other suggestions on to do what I want to do?

Zack Grossbart

unread,
Oct 1, 2013, 8:54:58 PM10/1/13
to spif...@googlegroups.com
My suggestion is to look at the actual content and that should give you a
good idea what to do.
Reply all
Reply to author
Forward
0 new messages