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:
https://gist.github.com/tmanOC/db82dba5d01612e92f7bd0b1891aac7c
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.








