AJAX Exception handling

B

bearsfoot

Hi,

I hope this is the correct group.

How should a HttpServlet include custom errors in in AJAX response.

I need to be able to control some client side behaviour based on the
error returned.

I have tried using cuctom status codes in the
HttpServletResponse.setStatus() and sendError() methods but I always
receieve a Status 500 such as in response below.
If an exception is thrown by the servlet a Status 500 is also
returned.

Thanks in advance.

===========================================
responseText = Error 500: null ErrorMessage
Error 500: Server caught unhandled exception from servlet
[testServlet]: ERROR: Cannot set header. Response
already committed.
responseXML.xml =
status = 500
statusText = Internal Server Error
 
S

Silvio Bierman

Hi,

I hope this is the correct group.

How should a HttpServlet include custom errors in in AJAX response.

I need to be able to control some client side behaviour based on the
error returned.

I have tried using cuctom status codes in the
HttpServletResponse.setStatus() and sendError() methods but I always
receieve a Status 500 such as in response below.
If an exception is thrown by the servlet a Status 500 is also
returned.

Thanks in advance.

===========================================
responseText = Error 500: null ErrorMessage
Error 500: Server caught unhandled exception from servlet
[testServlet]: ERROR: Cannot set header. Response
already committed.
responseXML.xml =
status = 500
statusText = Internal Server Error

At the time the exception occurs you have already written to the response.
Therefore, a response header can not be written.

The simplest but not neccesarily most efficient way to handle this is
generate the response to a seperate buffer
(StringWriter/ByteArrayOutputStream will do) and write that out at once if
no errrors occur or just set the error status if one does occur.

Regards,

Silvio Bierman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top