Add error message to json response

I have code that throws a specific type of exception:

throw new BadDataException("error message");

these exceptions are thrown inside a method whose response type is json. I have a configuration for this type of exception, for example:

<global-exception-mappings>
     <exception-mapping result="badDataError" exception="mypackage.BadDataException" />
</global-exception-mappings>

<result name="badDataError" type="json">
    <param name="statusCode">500</param>
</result>

I would like to add an exception message in the json response to show it to the user. Is there a way to match the exception message with the response when the 500 status code is returned. The ajax call would be something like this:

$.ajax(
{ 
   ...
    success: function(data, textStatus) {
         alert('Success'); 
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
         alert("Error");//I'd like to add here the reason (exception message)
    }
    ...
}
);

How can I automatically add a message about this exception to the HTTP 500 response? (if possible)

thank

+2
source share
2 answers

, , , . errorMessage HTTP 500 .

<result name="badDataError" type="httpheader">
                <param name="status">500</param>
                <param name="headers.errorMessage">${exception.message}</param>
</result>

ajax , .

error: function (XMLHttpRequest, textStatus, errorThrown) {
     var errorMessage = XMLHttpRequest.getResponseHeader('errorMessage'); 
     ....
}

, , .

+3

- , 500 ISE- JSON, . struts.xml( , httpheader ).

- Struts, AJAX. ExceptionMappingInterceptor @AjaxRequest ( ), JSON, . - .

.

+1

All Articles