Making Struts send 500 Internal Server Error when exceptions get thrown
Asked Answered
S

4

8

I'm working on an AJAX-enabled JavaScript frontend that makes calls to a Java backend written with Struts. My problem is that when the backend throws an exception, the client still sees a "200 OK" HTTP response code instead of "500 Internal Server Error" like one would expect.

This is tripping me up repeatedly because my 3rd-party client JavaScript library depends on HTTP status codes to determine if something is wrong with an AJAX call, as most modern libraries normally do. The errors pass undetected until my code blows up when it tries to parse what would normally be JSON.

I really want to avoid hacking my client JS library in order to gracefully handle these errors. So then, how can I make Struts give me a 500 status code when there's been an unhandled exception in the backend? Shouldn't this be Struts's default behavior?

Edit: The client-side code is irrelevant in this case. I am needing to fix the server so it sends the appropriate status code when unhandled exceptions happen. Thanks!

Stockist answered 12/3, 2012 at 22:8 Comment(0)
S
3

I figured out one way to do it. I'm not sure if it's the best or easiest, but it works. I found the Struts Exception Configuration guide and added the following to my <package> inside struts.xml:

<global-results>
  <result name="exception">/exception.jsp</result>
</global-results>

<global-exception-mappings>
  <exception-mapping exception="java.lang.Exception" result="exception" />
</global-exception-mappings>

This causes all unhandled exceptions to be redirected to /exception.jsp. And here are the JSP's contents:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="application/json; charset=UTF-8" %>
<% response.setStatus(500); %>
{"success": false,"errors": "<s:property value="%{exception.message}"/>"}

You'll note on the 3rd line that I manually set the response code to 500.

This gave me a new problem: exceptions were not being logged any more. As suggested in the aforementioned Struts guide, I solved this by adding the following to my <package> in struts.xml as well:

<interceptors>
    <interceptor-stack name="appDefaultStack">
        <interceptor-ref name="defaultStack">
            <param name="exception.logEnabled">true</param>
             <param name="exception.logLevel">ERROR</param>
        </interceptor-ref>
    </interceptor-stack>
</interceptors>

<default-interceptor-ref name="appDefaultStack" />

Struts: -1 for making such a simple and obvious feature NOT the default, and another -1 for making the solution so obtuse.

Stockist answered 15/3, 2012 at 16:26 Comment(0)
U
3

Update: I've fixed the <global-results> section (which was missing the httpheader tag)

I like your solution better, but here's another way you could do it if you didn't want to mess w/ setting the response header in the jsp.

  1. In struts.xml, create a global exception mapping from "java.lang.Exception" to a result named "exception"
  2. In struts.xml, create a global result which maps "exception" to a httpheader result with a value of 500.
  3. In web.xml, create an error-page entry that maps error code 500 to your error page location.

So, for struts.xml:

<global-exception-mappings>
    <exception-mapping exception="java.lang.Exception" result="exception" />
</global-exception-mappings>

<global-results>
    <result name="exception" type="httpheader">
        <param name="error">500</param>
    </result>
</global-results>

And for web.xml

<error-page>
    <error-code>500</error-code>
    <location>/exception.jsp</location>
</error-page>

This works but has some big downsides. The servlet container is rendering your error page (not struts) so you wont have access to the original error message or the struts valueStack. Exception logging will still work, however (if you have enabled it).

As an aside, I too find it baffling that struts makes this so difficult. In every other framework I've dealt with the concept of returning an error page and an error code is pretty trivial to implement.

Ugaritic answered 25/3, 2013 at 20:43 Comment(0)
R
0

The 500 Internal Server Error is a server-side error, meaning the problem probably isn't with your computer or Internet connection but instead is a problem with the web site's server.

There is noting to handle this in the client side. It is on the Web Server (HTTP server) configured in handling the requests.

Roumell answered 18/12, 2012 at 9:47 Comment(1)
-1. I think by the way I wrote my question, it's clear that I already understand what a 500 Internal Server Error is, as well as the nature of client-server interactions. I was looking for information on how to make Struts (on my server) behave the way I wanted it to.Stockist
K
-1

You didnt mention which JS library you are using. I would suggest read the documentation carefully. If it has some kind of callback method, it must have some responseText other than responseCode. Whenever some validation exception happens in server-side, pass a error message too. Just don't rely on throwing some exceptions, because you are using AJAX. You can track that responseText for showing some message like "validation failed for this reason, please try again". Agree ?

Otherwise throw WebApplicationException before the response is committed:

throw new javax.ws.rs.WebApplicationException();  // or by any other constructor

Here is the jar

Korte answered 14/3, 2012 at 5:34 Comment(3)
The client-side library is ExtJS but that doesn't matter. The server is what's having the problem. Struts sends an HTML error report and a "200 OK" status code when there's an unhandled exception on the server side, and that's what I'm trying to fix. The HTML I can handle, but Struts has got to be able to send code 500 when errors happen, not code 200. Thanks!Stockist
WebApplicationException() causes an HTTP 500 error code. did you try throwing this one ?Korte
On the server side, I want to avoid having to insert try/catch blocks everywhere. I just need some kind of configuration that tells Struts, "do the Right Thing(tm) and give me a 500 response code whenever you send these silly Problem Reports."Stockist

© 2022 - 2024 — McMap. All rights reserved.