I'm having an issue with an $http Promise not being bubbled up from the service where it is being executed to any place where the service method is called.
I have an .authozire() method in the service that returns the $http Promise, as follows:
// auth.service.js
function authorize(authParams) {
var request = {
method: 'POST',
url: _apiUrl + 'oauth/token',
data: authParams
};
return $http(request)
.then(
function successHandler(response) {
// Correctly executed when success
$log.debug('authService :: authorize => success', response);
},
function errorHandler(response) {
// Correctly executed when error
$log.debug('authService :: authorize => error', response);
}
);
}
The success and error methods above (please not that I'm already not using the .success() and .error() methods as they are deprecated) work just fine in each case, but it doesn't work as expected when I call this method from a controller, like this:
// signin.controller.js
function submit() {
authService.authorize(vm.formData)
.then(
function successHandler(response) {
// Always executed even when there's an error
$log.debug('SignInController :: submit :: authService.authorize() => success', response);
},
function errorHandler(response) {
// NEVER executed
$log.debug('SignInController :: submit :: authService.authorize() => error', response);
}
);
}
The first method successHandler is always called, even when there's an error and the service executed its own errorHandler.
It seems like the Promise being returned by the service it's just a simple Promise that accepts .then(), but doesn't differentiate success and fail like explained in the docs:
Returns a Promise that will be resolved to a response object when the request succeeds or fails.
So... Am I missing something? Does anyone had a problem like this before?
Thanks!
service = {authorize: authorise}; return service;and in the controller:vm.submit = submit;