
A Kill Script For Processes That Are Using TCP/UDP Ports
April 24, 2020
Embedding Website Links Into Your Android App
June 25, 2020
App Insights is great for monitoring services, logging metrics and analysing traces and exceptions. However I recently came across an issue where unhandle promises rejections showed up in App Insights as [object Promise]. Which is fine however the stack trace was not very helpful, showing that the error was thrown in the appinsights package itself.
The auto exception tracking feature of the appinsights node module catches unhandled Promise rejections which is great, but it doesn’t extract the message from them. In fact it checks whether the object it receives for tracking is an Error and when it finds out that it is not error, it throws an internal Error which is where the stack trace comes from.
To show the info inside a promise rejection, I made a wrapper around the trackException function on the defaultClient:
| appInsights = require("applicationinsights"); | |
| let exceptionTrack = appInsights.defaultClient.trackException; | |
| appInsights.defaultClient.trackException = function myExceptionTracker(telemetry) { | |
| let exception = telemetry.exception | |
| //How to test for a Promise | |
| if (Promise.resolve(exception) == exception) { | |
| exception.then(res => { | |
| telemetry.exception = res; | |
| exceptionTrack.call(appInsights.defaultClient, telemetry); | |
| }, err => { | |
| telemetry.exception = err; | |
| exceptionTrack.call(appInsights.defaultClient, telemetry); | |
| }).catch(error => { | |
| telemetry.exception = error; | |
| exceptionTrack.call(appInsights.defaultClient, telemetry); | |
| }) | |
| } else { | |
| exceptionTrack.call(appInsights.defaultClient, telemetry); | |
| } | |
| } |
The trackException wrapper function receives the object in telemetry.exception and tests whether it is a Promise and handles it accordingly, extracting the message from the Promise and changing the telemetry object before passing it on to the original trackException function.






