0

I am using Struts 2 user-defined result that is used with an AJAX request.

When it throws an exception, I get a "Struts Problem Report" in html format as a response. This is not very useful. How can I intelligently handle such an exception -- either call the appropriate javascript errorResponse function or take the user to another page?

public class MyResult implements Result {

    @Override
    public void execute(ActionInvocation invocation) {

        if (invocation.getStack().findValue("data") == null) {
            throw MyException("Data is bad.");
        }

        PrintWriter responseStream =
            ServletActionContext.getResponse().getWriter();
        responseStream.println("Data is good.");
        responseStream.close();
    }
}
Victor Lyuboslavsky
  • 9,882
  • 25
  • 87
  • 134

1 Answers1

0

Either declare an exception handling result or stream something useful back.

Gabriel Bleu
  • 9,703
  • 2
  • 30
  • 43
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • This is an AJAX request, so the page won't be reloaded. What kind of exception handling result can I define that would provide any useful info to the user? – Victor Lyuboslavsky Mar 06 '12 at 18:36
  • @Victorb Exception info can be serialized to JSON, you can set HTTP codes, etc. and act on it in the JavaScript callback. – Dave Newton Mar 06 '12 at 19:15
  • Right. So messing with Struts exception handling configs isn't going to help too much here unless I have a custom exception handling result. The best way is to stream something useful back -- like a custom error or serialized JSON exception. – Victor Lyuboslavsky Mar 06 '12 at 22:31
  • @Victorb It's not really particularly "custom", but ok. Normally you wouldn't want to just stream back a chunk of text, you'd stream back either HTML or JSON, and possibly an HTTP error code, depending on how you want the client side to deal with things. Either way, it can be handled in a pretty general way on both server and client side. – Dave Newton Mar 06 '12 at 22:35