1

In a web controller I have an @ExceptionHandler implementation to handle a certain type of device exception I can get. It looks like this :

    @ExceptionHandler(DeviceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
public void handleException(DeviceException ex) {
    log.error("controller caught exception " + ex.getMessage());
            return ex.getMessage();
}    

What I want to achieve is to have this return the Exception's message when a DeviceException is thrown.

On the client side I have this jQuery code, current problem is I can't seem to get any of the ex.getMessage() content in the error callback, that's what I need to solve.

        $.ajax( {
        type: "GET",
        url: "<c:url value="/x/y/status"/>.json",
        dataType: "json",
        cache: false,
        success: function ( data ){
          // use the data
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // problem : all 3 of the data below (responseText etc.) are blank when I've thrown a DeviceException

            alert('jqXHR.responseText = ', jqXHR.responseText);
            alert('textStatus = ', textStatus);
            alert('errorThrown = ', errorThrown);
        }
    } );
1
  • I added an update based on today's testing, I need to work out how to get the exception data out of the args passed into the error handler Commented Jul 26, 2011 at 23:37

2 Answers 2

1

@ResponseStatus has a value field too. This can be used to set any custom messages. Check the documentation @ http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html

Sign up to request clarification or add additional context in comments.

Comments

0

The @ExceptionHandler mechanism is very flexible with the parameters and return types the method annotated with it can have.

You can use some combination of the HttpServletResponse object or returning a Model and/or View to return something meaningful to the AJAX caller - for example, a specific response code, or write something to the output stream you can parse in JavaScript, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.