I have a factory in Angular, where I want to add an alert if an error is encountered.
Here's my call to the factory:
gradeService.assignGrade(requestData).then(Controller.populateResponseObject, Controller.error);
where Controller
just a this for the current controller: var Controller = this
.
When I try to trigger the error in the UI, a 500 Server Error is encountered(as expected), but it goes to populateResponseObject
, not to error
. How to I get the service to return error?
Here's the service code:
app.factory('gradeService', function ($http) {
var baseUrl = "http://localhost:8080";
var add = function (request) {
var requestUrl = baseUrl + "/grade/new";
return $http.post(requestUrl, request)
.then(function (responseSuccess) {
return responseSuccess.data;
},
function (responseError) {
return responseError.data;
});
};
return {
assignGrade: add
};
});
Here's the relevant error
code:
Controller.error = function (error) {
// ... some code
else if(error.status === 500) {
if(error.exception === "org.springframework.dao.DataIntegrityViolationException") alert("Error: this person has already been graded for this month. Grade was not saved.");
else if(error.exception === "java.sql.SQLException") alert("Error establishing connection with database. Please try again later.");
else alert("Error: " + error.message + ": Generic Server Error.");
}
};
I'm using Spring for the backend code.
Any help?