【问题标题】:Problem tracking custom error with Application Insights: TypeError: Cannot read property 'stack' of undefined使用 Application Insights 跟踪自定义错误时出现问题:TypeError:无法读取未定义的属性“堆栈”
【发布时间】:2020-08-24 20:43:56
【问题描述】:

我编写了一个自定义错误类,并希望使用 Azure Application Insights 跟踪错误。我正在使用 Node.js。当我尝试跟踪自定义错误时,我收到以下错误:

TypeError: Cannot read property 'stack' of undefined
    at Function.EnvelopeFactory.createExceptionData (/Users/liam/Desktop/CustomError/node_modules/applicationinsights/out/Library/EnvelopeFactory.js:145:40)
    at Function.EnvelopeFactory.createEnvelope (/Users/liam/Desktop/CustomError/node_modules/applicationinsights/out/Library/EnvelopeFactory.js:32:40)
    at NodeClient.TelemetryClient.track (/Users/liam/Desktop/CustomError/node_modules/applicationinsights/out/Library/TelemetryClient.js:116:44)
    at NodeClient.TelemetryClient.trackException (/Users/liam/Desktop/CustomError/node_modules/applicationinsights/out/Library/TelemetryClient.js:69:14)
    at Object.<anonymous> (/Users/liam/Desktop/CustomError/app.js:10:8)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

代码如下:

class CustomError extends Error {  
    constructor (message) {
      super(message)
      
      Error.captureStackTrace(this, this.constructor);
      this.name = this.constructor.name;
      this.status = 404;
    }
  
    statusCode() {
      return this.status
    }
}

const appInsights = require('applicationinsights');
appInsights.setup('MY_KEY').start();
let client = appInsights.defaultClient;

const customError = require('./exceptions');
let newError = new customError('The custom error was thrown')

client.trackException(newError);

我根据其他在线资源对 TelemetryClient 对象进行了一些研究,但我认为这不是正确的方向。

我将此资源用作指南:https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#trackexception

【问题讨论】:

    标签: node.js error-handling azure-application-insights


    【解决方案1】:

    我能够通过将键值对传递给 trackException 来解决这个问题,其中键是异常,值是错误:

    client.trackException({exception: new customError('This is a custom error')});
    

    完整代码:

    class CustomError extends Error {  
        constructor (message) {
          super(message)
          
          Error.captureStackTrace(this, this.constructor);
          this.name = this.constructor.name;
          this.status = 404;
        }
      
        statusCode() {
          return this.status
        }
    }
    
    const appInsights = require('applicationinsights');
    appInsights.setup('MY_KEY').start();
    let client = appInsights.defaultClient;
    
    client.trackException({exception: new CustomError('This is a custom error')});
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 2016-03-18
      • 2020-05-09
      • 2020-07-24
      • 2021-11-17
      相关资源
      最近更新 更多