Rebecca Tauber Rebecca Tauber - 2 months ago 5x
Javascript Question

jQuery alerts 'undefined' on error instead of Java/Spring Exception details

I've been using Backbone in my front end interface for a Java program backed with Spring. On the page, the user submits a file which then gets processed, and they get a download in return. When everything runs fine with the Java code, they get the download as they are supposed to. But, if something goes wrong, it stops, and the page returns an alert box that says 'undefined'.

I would like to be able to return interactive errors so that the user can know what went wrong. I have a RestExceptionHandler class that looks like so, and I would like to return the exceptions to the user in the alert box.

public abstract class RestExceptionHandler {

@ExceptionHandler({ Exception.class })
public ResponseEntity<String> handleGeneralException(final Exception exception) {
logException("General Exception : ", exception);
return new ResponseEntity<String>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);

@ExceptionHandler({ IllegalArgumentException.class, JsonException.class })
public ResponseEntity<String> handleBadRequestException(final Exception exception) {
logException("Bad Request Exception : ", exception);
return new ResponseEntity<String>(exception.getMessage(), HttpStatus.BAD_REQUEST);

private void logException(final String info, final Exception exception) {
log.error(info + exception.getMessage() + Throwables.getStackTraceAsString(exception));

For a little background on the JavaScript, I wrote an event function for when the user clicks submit. It gets the input from the page, sets it to the Backbone model, then...

// post to output and return download
model.url = window.location + "output";{},
error: function (model, response) {
success: function (model, response) {
// download stuff goes here ...

Here's the Spring Controller...

public class ValidateController extends RestExceptionHandler {

private OntologyValidate validate;

@RequestMapping(value="/output", method= RequestMethod.POST)
public @ResponseBody Object graph(@RequestBody String body) throws Exception {

JSONObject jo = new JSONObject(body);

// get user input arguments from body
JSONArray JSONinputFiles = jo.getJSONArray("inputFile");
JSONArray JSONfileFormats = jo.getJSONArray("fileFormat");
JSONArray JSONfileNames = jo.getJSONArray("fileName");

String label = jo.getString("label");
String description = jo.getString("description");
String fin = "";

// Do processing stuff here

jo.put("results", fin);
return new ResponseEntity<Object>(jo.toString(), HttpStatus.OK);


I'm happy to provide any additional code necessary.


Answered my own question!

Rather than trying to get the information from response on error, I needed to get it from xhr. The exception handler does not need to return a response entity, just a string with the exception (which also looks much cleaner)

@ExceptionHandler({ Exception.class })
public String handleGeneralException(final Exception e) {
    logException("General Exception : ", e);
    return e.toString();

Then my JavaScript alerts the message if the HttpStatus is not OK (not sure if the model and response are necessary):

error: function (model, response, xhr) {
    var json = JSON.stringify(xhr.xhr.responseText);
    json = json.replace(/"/g, "");

Stringify returns the message in double quotes, so I just replaced them with nothing for a cleaner message. Nothing had to be changed in the controller.