如何处理AngularJS中的工厂服务错误

时间:2023-01-24 11:19:03

Factory code

app.factory('abcFactory', function ($http, Config, $log) {
    var serviceURL = Config.baseURL + '/results';
    return{
        results:function() {
            var promise = $http({
                method: 'GET',
                url: serviceURL,
                timeout: Config.httpTimeout
            }).then(function(response) {
                    return response;  

                },  function(reason) {
                    $log.error("Request Failed: ", reason)
                });

            return promise;
        }
    }
});

Controller code

app.controller('abcController',function ($scope, $log,abcFactory, Config)
{

        abcFactory.results(function(data){
            $scope.results =data;
        },function(response)
        {
          console.log("response"+response);

          if(response.status === 0)
          {
              console.log("gotcha");
               $scope.serviceTimedoutError(response.data, response.status, response.config);
          } else
          {
                console.log("gotcha");
                $scope.serviceFailure(response.data, response.status, response.config);
          }


        });
});

$scope.results populates fine when service has returned good response.

当服务返回良好响应时,$ scope.results会填充。

And, when there is an error I can see on console the error message coming from factory code's log saying "Request Failed: blah blah ".

并且,当出现错误时,我可以在控制台上看到来自工厂代码日志的错误消息“请求失败:等等等等”。

My issue: When there is an error, why I am not seeing error message in controller, it is not even printing "gotcha" in browser console. I need error details in controller, so that I can show it on view. I don't want to pass $scope in factory.

我的问题:当出现错误时,为什么我没有在控制器中看到错误消息,它甚至没有在浏览器控制台中打印“gotcha”。我需要控制器中的错误详细信息,以便我可以在视图中显示它。我不想在工厂通过$ scope。

What wrong I am doing in Controller code?

我在Controller代码中做了什么错?

If I am not wrong, I am doing something in line of AngularJS Failed Resource GET but not getting desired result.

如果我没有错,我正在根据AngularJS Failed Resource GET做一些事情,但没有得到理想的结果。

2 个解决方案

#1


4  

It looks like you want the results function to take callbacks, but its arguments list is currently empty. You are returning the promis though.

看起来您希望结果函数采用回调,但其参数列表当前为空。你回来了promis。

So either change the call in the controller to:

因此要么将控制器中的调用更改为:

abcFactory.results()
  .success(function() { ... })
  .error(function() { ... })

Or change the results function itself:

或者更改结果函数本身:

results: function(successCallback, errorCallback) {
  var promise = $http(...).success(successCallback).error(errorCallback);
  return promise;
}

#2


1  

Deprecation Notice The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

弃用通知已弃用$ http遗留承诺方法成功与错误。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。

#1


4  

It looks like you want the results function to take callbacks, but its arguments list is currently empty. You are returning the promis though.

看起来您希望结果函数采用回调,但其参数列表当前为空。你回来了promis。

So either change the call in the controller to:

因此要么将控制器中的调用更改为:

abcFactory.results()
  .success(function() { ... })
  .error(function() { ... })

Or change the results function itself:

或者更改结果函数本身:

results: function(successCallback, errorCallback) {
  var promise = $http(...).success(successCallback).error(errorCallback);
  return promise;
}

#2


1  

Deprecation Notice The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

弃用通知已弃用$ http遗留承诺方法成功与错误。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。