2014-03-03 56 views
1

这是我想什么:有一个模块或服务,或什么的,它提供了全局错误处理程序为:在角表达合理的做法对全球错误处理的角度

  1. 错误(所以一切由$exceptionHandler服务抓)的HTTP请求(所以一切都在requestError截获
  2. 错误和responseError作为defined by an $httpProvider
  3. 错误触发window.onerror(我相信这将包括错误发生的“外”角度,例如,从独立第三方PA RTY库)

理想情况下,我想捕获所有这些错误,让他们提供给我包含在HTML角的应用程序,或许通过将它们添加到$rootScope。但是,我迄今为止完成的微薄尝试已经证明有些复杂,并不完全成功。

这里有一个备用,低于理想,但似乎功能的方法,我得到工作:外面角,设置一个全球性的处理W/window.onerror;那么当遇到上述第1项和第2项中的错误时,实际上会出现throw错误(导致所有内容都使其成为window.onerror)。

我当然不会为这个解决方案感到骄傲,不仅因为它很黑,而且因为它阻止我使用Angular显示错误(而不是我自己填充DOM),并且因为它剥去了有用的错误信息(因为捕获的值w/onerror只是一个普通的字符串,与具有信息属性的对象相反)。

回答

1

我如何被处理错误

var pageModule = angular.module('pageModule',[]) 
.service('myService',function($http) { 
    return { 
     someHttpCall : function() { 
      return $http.get('myHttpCall'); 
     } 
    } 
}) 
.controller('parentCtrl',function($scope,myService) { 
    myService.someHttpCall() 
    .success(function(response) { 
     // 
    }) 
    .error(function(response) { 
     $scope.$emit('errorEvent', { 
      type : 'networkError', 
      message : 'There was a network error with this ajax call' 
     }); 

    }); 

    $scope.someFunction = function() { 
     if(error) { 
      $scope.$emit('errorEvent', { 
       type : 'myErrorType', 
       message : 'My message about error here' 
      }); 
     } 
    } 
}) 
.controller('childCtrl',function($scope) { 
    $scope.someFunctionInChildScope = function() { 
     if(error) { 
      $scope.$emit('errorEvent', { 
       type : 'myOtherErrorType', 
       message : 'My other message about error here' 
      }); 
     } 
    } 
}) 
.directive('myErrorMessage',function($rootScope) { 
    return { 
     link : function($scope,$element,$attrs) { 
      $rootScope.$on('errorEvent',function(event,errorObj) { 
       //compile and append an error here.. based on errorObj 
      }); 
     } 
    } 

}); 
1

在我的应用程序使用的第二方法使用拦截器用于错误处理的一个粗略的例子。

我写了一个服务来覆盖拦截器responseError,并且我在$ rootScope上定义了一个“openErrorModel”方法,当有错误时打开带有错误消息的错误模型。

这将更清洁和模块化。你可以通过这种方式避免重复。

示例代码:

(function (global) { 
    "use strict"; 

    angular.module("TestAPp").factory('httpInterceptor', ["$rootScope", "$q", function ($rootScope, $q) { 
     return { 
     responseError: function (response) { 
      /* Open Error model and display error */ 
      $rootScope.openErrorModel(response); 
      /* reject deffered object so that it'll reach error block , else it'll execute success function */ 
      return $q.reject(response); 
     } 
     }; 
    }]); 

}(this)); 

//注册interceprtor

(function (global) { 
    "use strict"; 

    angular.module("TestApp", []) 
      .config([ '$httpProvider',function ($httpProvider) { 
       /* Register the interceptor */ 
       $httpProvider.interceptors.push('httpInterceptor'); 

    }]); 

}(this)); 

PS:我openErrorModel定义

$rootScope.openErrorModel = function (error) { 
     $rootScope.errorMessage = error.data; 
     $('#errorModal').modal('show'); 
}; 

您可以参考Error Handling in angular获取更多信息。

+0

你在哪里定义“errorModal”? – CuriousCoder

+0

我将其添加到DOM。它是一个引导模式,只有错误消息被添加到“$ rootScope.errorMessage”中。 – Naruto