Currently I am utilizing angularJS and spring 3.2.4 to handle REST exceptions in a particular way, shown below:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorFormInfo handleMethodArgumentNotValid(HttpServletRequest req, MethodArgumentNotValidException ex, HttpServletResponse response) {
String errorMessage = "gender string is out of range";
String errorURL = req.getRequestURL().toString();
System.out.println("Throwing exception from the exception handler");
ErrorFormInfo errorInfo = new ErrorFormInfo(errorURL, errorMessage);
return errorInfo;
}
Whenever an argument validation failure occurs, it results in throwing a MethodArgumentNotValidException. However, I have noticed that while Spring 3.2 automatically converts objects into JSON format, I am unable to access the object in the error block of AngularJS without encountering an error stating "Response is undefined" as shown below:
completeRequest(callback=done(status, response, headersString), status=400, response="{"errorMessage":"http:/...string is out of range"}", headersString="Server: Apache-Coyote/1...MT\r\nConnection: close\r\n")angular-1.0.7.js (line 9333)
onreadystatechange()
I suspect that the issue lies in the object not being converted into JSON due to a peculiar header string.
Below is my JavaScript code snippet:
$http.post('rest/create?cd=' + (new Date()).getTime(),
XYZ
.success(function(data) {
alert('Data added successfully');
}).error(function(data) {
var errorInfo = data;
alert("data from server side"+errorInfo.errorMessage);
alert("Unable to process");
});
If anyone could offer assistance on this matter, it would be greatly appreciated. Thank you in advance.