2016-04-12 14 views
0

Tüm uygulama özel durumlarını bir uyarı olarak göstermek için istisna işleyicisiyle birlikte angular-ui-boostrap kullanıyorum. Uyarı, zamanaşımı için reddetti. (Yeşim yazılmış)angujar digest hata döngüsü

$provide.decorator "$exceptionHandler", ['$delegate', 'alerts', ($delegate, alerts) -> 
    (exception, cause) -> 
    $delegate(exception, cause) 
    if (alerts.elements.length < 5) 
     alerts.add('danger', exception.message) 
] 

ve uyarı tanım:

uib-alert.animate-leave(ng-repeat="alert in c.alerts.elements" type="{{alert.type}}" close="c.alerts.close($index)" dismiss-on-timeout='5000') {{alert.message}} 

varsa sorun olduğunu özetle sırasında bir hata olduğunu burada

istisna yumruk (kahve yazılmış) kodu bulunuyor Döngü sonsuz bir döngü olsun. Çünkü bir hata oluştuğunda yeni bir uyarı ekliyorum, böylece sindirim döngüsü tekrar oluyor, böylece yeni hata tetikleniyor. uyarı alın en görevden hata tetiklenir, bu nedenle yeni uyarı vb ilave edilir ve zaman

Ve aynı olmuyor.

Bunu nasıl çözebilirim? Tüm sindirim döngüsünü tetiklemeden uyarıları güncellemek mümkün mü? Ancak uyarıları hala ekranda güncelleyin.

cevap

1

Muhtemelen bu konuda hiçbir yolu yoktur. Ama yapabildiğiniz şey, açısalların hata işleyicisinin üzerine yazılması ve belirli bir süre boyunca hataların sayısının üzerine yazılması ve sindirim döngüsünün (düz JS'deki tüm kod) neden olduğu sonsuz bir hata döngüsü varsa, uygulamanın durdurulmasıdır:

function ExceptionService($log,$injector) { 
 

 
     var errorLog = {}; 
 
     var applicationStopped = false; 
 

 
     /** 
 
     * override method to catch errors 
 
     * @param exception 
 
     * @param cause 
 
     */ 
 
     function error(exception, cause) { 
 
      // forward output to console 
 
      $log.error.apply($log, arguments); 
 
      if (!applicationStopped) { 
 
       var cycleBreaked = _digestCycleBreakerCheck(); 
 
       if (!cycleBreaked) { 
 
       //not many errors yet, just print to console or do nothing 
 
       } else { 
 
       //p.e. stop the whole angular application by destroying the rootScope 
 
       } 
 
      } 
 
     } 
 

 
     function _digestCycleBreakerCheck() { 
 
      var cycleBreaked = false; 
 
      if (Object.keys(errorLog).length === 0) { 
 
       _resetErrorLog(); 
 
      } else { 
 
       if (errorLog.lastErrorDate >= (_getCurrentTs() - 15)) { 
 
        errorLog.count += 1; 
 
        errorLog.lastErrorDate = _getCurrentTs(); 
 
        if (errorLog.count >= 10) { 
 
         //break digest cycle! 
 
         cycleBreaked = true; 
 
        } 
 
       } else { 
 
        //it has been the first error after more than x seconds 
 
        _resetErrorLog(); 
 
       } 
 
      } 
 
      return cycleBreaked; 
 
     } 
 

 
     function _resetErrorLog() { 
 
      errorLog.count = 1; 
 
      errorLog.lastErrorDate = _getCurrentTs(); 
 
     } 
 

 
     function _getCurrentTs() { 
 
      return Math.floor(Date.now()/1000); 
 
     } 
 

 
     return (error); 
 
    }

bkz: http://engineering.talis.com/articles/client-side-error-logging/