Unhandled rejection with non-errors from sentry js

It seems that sentry’s js lib reports unhandled rejections, where the rejection value is not an Error, with a ‘type’ and ‘value’ of ‘[undefined]’. By contrast, non-Error objects that are thrown synchronously at least appear to have the value populated (perhaps by .toString()). I spent a significant amount of time trying to figure out what our [undefined] [undefined] in Sentry was, and a few features might have made this quicker:

  1. Displaying in the UI the ‘exception.mechanism.type’, which the js library is reporting as ‘onunhandledrejection’
  2. Serializing the value for non-objects the same on unhandled rejection as on uncaught thrown error
  3. Reporting, when the value is not an Error (and so .name doesn’t necessarily exist), something indicative of the type (e.g. .constructor.name).

In case it’s helpful for anyone else, I accomplished most of the above by configuring sentryJS:

Sentry.init({
beforeSend: function(body){
  if (body.exception.mechanism.type == 'onunhandledrejection') {
    for (var i = 0; i < body.exception.values.length; i++) {
      if (body.exception.values[i].value == undefined) {
        var originalException = arguments[i + 1].originalException;
        if (originalException) {
          body.exception.values[i].type = originalException.constructor.name + ' [unhandled rejection]';
          body.exception.values[i].value = originalException.toString();
        }
      }
    }
  }
  return body;
},