title: Problem tracking custom error with Application Insights: TypeError: Cannot read property 'stack' of undefined
body: <p>I have written a custom error class and would like to track the error using Azure Application Insights. I am using Node.js. When I try tracking the custom error I get the following error:</p>
  <pre><code>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.&lt;anonymous&gt; (/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)
  </code></pre>
  <p>Here is the code:</p>
  <pre><code>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);
  </code></pre>
  <p>I have done some research into TelemetryClient objects based on other resources online but I don't think that is the right direction.</p>
  <p>I am using this resource as a guide: <a href="https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#trackexception" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#trackexception</a></p>