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!
Delete your browser's cookies. You can correct some 500 Internal Server Error issues by deleting the cookies associated with the site on which you're getting the error. After removing the cookie(s), restart the browser and try again. Troubleshoot as a 504 Gateway Timeout error instead.
There are two methods for handing uncaught exceptions in Struts: Global exception handling: specifies exception mappings (exception type - view name) which apply to all action classes in a Struts package. Exception handling per action: specifies exception mappings which apply to a specific action class.
This can be a php timeout. In such cases, the webserver will return a 500 Internal Server Error. We can fix this error by increasing timeout values or setting other appropriate timeout options so that the remote server will not return a timeout error but wait for the request to be processed.
To be clear, if the error encountered by the server was due to CLIENT input, then this is clearly a CLIENT error and should be handled with a 4xx response code. The expectation is that the client will correct the error in their request and resubmit.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With