简体   繁体   English

angularjs 捕获触发 $exceptionHandler 错误的 html 元素和数据?

[英]angularjs capture html element and data that triggered an $exceptionHandler error?

My app has a working $exceptionHandler , if an angularJS error occurs, it captures the error and emails the data back to me.我的应用程序有一个有效的$exceptionHandler ,如果发生 angularJS 错误,它会捕获错误并将数据通过电子邮件发送给我。 But its not capturing the HTML element that triggered the $scope function nor is it capturing the data that was passed to the function that caused the error to trigger.但它没有捕获触发 $scope 函数的 HTML 元素,也没有捕获传递给导致错误触发的函数的数据。

To elaborate, I have multiple pages within my app that use that use the same controller which in turn are using the same functions.详细地说,我的应用程序中有多个页面使用相同的控制器,而这些控制器又使用相同的功能。 As an example, the same validation monitorLength function is used on multiple pages where the user is inputting data, in some cases its on an <input type='tel' ng-keydown="monitorLength($event,this.id,phoneLength,1)"> field, in other cases on a <input type='number' ng-keydown="monitorLength($event,this.id,postalLength,2)"> field and in some cases the function is being called by another function.例如,在用户输入数据的多个页面上使用相同的验证monitorLength函数,在某些情况下,它在<input type='tel' ng-keydown="monitorLength($event,this.id,phoneLength,1)">字段,在其他情况下在<input type='number' ng-keydown="monitorLength($event,this.id,postalLength,2)">字段上,在某些情况下该函数被另一个调用功能。

But when the error/exception is thrown, all it tells me is something like the following making it almost impossible to determine which page/template/input field the function was used on or what the data was that caused the function to error:但是当错误/异常被抛出时,它告诉我的只是类似于下面的内容,这使得几乎不可能确定函数在哪个页面/模板/输入字段上使用或导致函数出错的数据是什么:

Exception: undefined is not an object (evaluating 'mLengths.length')
Stack: ionic://localhost/js/controllers.js:1291:47

In the error handling, how can I also get A) what HTML input/button/field that called the function AND B) get the data that was passed to the function that caused it to error?在错误处理中,我怎样才能获得 A) 调用函数的 HTML 输入/按钮/字段和 B) 获得传递给导致错误的函数的数据?

Using the example mentioned, here is the monitorLength function:使用提到的示例,这里是 monitorLength 函数:

  var permitChars = [8,35,36,46] ;  //allow delete and backspace
  var permitZip = [32,109,189] ;  //allow characters in zip code
  $scope.monitorLength = function($event,item,itemID,mLengths,deny) {
    //deny = 1 = phone
    //       2 = zip
    var keyCode = $event.keyCode || $event.charCode ;
    if (!permitChars.includes(keyCode)) {
      if (deny == 1 && isNaN(String.fromCharCode(keyCode))) {
        $scope.errMsg = "Non-numeric characters are not permitted.  IE: + . , - etc" ;
        $scope.errColor = "red" ;
        $event.preventDefault() ;
        return ;
      } 
      
      if (deny == 2 && (isNaN(String.fromCharCode(keyCode)) || permitZip.includes(keyCode))) {
        $scope.errMsg = "Only numbers, space and dash/minus characters are permitted" ;
        $scope.errColor = "red" ;
        $event.preventDefault() ;
        return ;
      } 

      var len = $scope[item][itemID] ;
      if (deny == 1 && (/^0/.test(len) || /^1/.test(len))) {
        $scope.errMsg = "Country/Prefix code already selected, just enter main phone number" ;  
        $scope.errColor = "red" ;
        $event.preventDefault() ;
        return ;
      } 
      
      var maxLength = parseInt(mLengths[mLengths.length-1]) ;
      if (len && (len.toString().length > maxLength)) {
        $scope.errMsg = "Max length is " +maxLength+ " chars for this field"
        $scope.errColor = "red" ;
        $event.preventDefault() ;
        return ;
      }
    } else {
      $scope.errMsg = "" ;
    }
  }

And here is my error/exceptionHandler:这是我的错误/异常处理程序:

.config(function ($provide,apiServiceProvider) {
  $provide.decorator('$exceptionHandler', function($delegate) {
    return function (exception, cause) {
        // return emailApi(eType,cat,topic,subject,msg) ;
        var msg = "\nDateTime: " + new Date() ;
        msg = msg+ "\nAppVersion: " +appFullVer ;
        msg = msg + "\nException: " +exception.message ;
        msg = msg + "\n<br>Stack: " +exception.stack ;
        if (cause) {
          msg = msg + "\n<br>Cause: " +cause ;
        } else {
          msg = msg + "\n<br>Cause: none passed" ;
        }
        if (httpErrorID == 1) {
          msg = msg+ "\nHTTP Error: " +httpErrorMsg ;
          httpErrorID = 0 ;
        }
        if (errTesting == 0) {
          apiServiceProvider.$get().mail(5,"AppError","Bug","App Angular JS Error",msg) ;
        } else {
          console.log(msg) ;
        }
      }
  }) ;
})

Is mLengths ever undefined , etc? mLengths是否曾经undefined等?

Does setting a value for mLengths change anything?mLengths设置一个值mLengths改变什么吗?

$scope.mLengths = mLengths || [];

Also, refactoring like so另外,像这样重构

const index = mLengths.length - 1;
parseInt(mLengths[index])

could be useful in debugging.在调试中可能很有用。

I don't expect my code to work, only to express ideas.我不希望我的代码能够工作,只是为了表达想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM