I have a Java Spring MVC controller that can throw an exception. I have an @ExceptionHandler setup to handle these errors, and I'd like to use it to return the exception's message to the caller.
The server code is:
@ExceptionHandler(DeviceException.class)
@ResponseStatus(HttpStatus.METHOD_FAILURE)
@ResponseBody
public String handleException(DeviceException ex) {
return ex.getMessage();
}
I have tested throwing a DeviceException and getting the result using Curl from the CLI, the exception's message is being returned in the response body. I can't get the jQuery .error handler to display it however, my handler code is:
error: function(jqXHR, textStatus, errorThrown) {
// problem, the data is always blank
alert('jqXHR.responseText = ', jqXHR.responseText);
}
All I ever get for the responseText is an empty string. How do I get the string returned by the @ExceptionHandler to display in the jQuery error handler?
Try something like this:
var msg = '<div style="padding:20px;">';
msg += "We're sorry, there was an error during your request. Please refresh the page, then try again. If your problem persists, please contact technical support. <br /><br /> <b>Cause:</b> <br /> ";
switch (textStatus) {
case ('timeout'):
msg += 'Request timed out.';
break;
case ('error'):
msg += 'Error status returned.';
break;
case ('abort'):
msg += 'Request was aborted.';
break;
case ('parseerror'):
msg += 'There was an error parsing your request.';
break;
default:
msg += 'No status returned.';
break;
}
$(this).html(msg + ' <br /><br /> ' + errorThrown + ' <br /></div>');
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