Catch a rejected promise and log custom error message + reason

What’s the recommended approach to catching a browser’s Promise rejection and logging it with a custom message?

e.g.

.catch(function(err) {
  Raven.captureException('Error in user creation code', err);
}

However I’m not sure that captureException supports two parameters, or how much useful information that err will have. e.g. I’ve seen references to err.reason, err.stack, etc.

What you’re looking for is captureMessage here, as you dont have an actual Error object (which is what the captureException call requires).

Raven.captureMessage('foo bar', {extra: {reason: err.reason}})

Thanks for the quick advice. For cases where I don’t have my own catch, what would be the best way to configure Raven?

e.g.

  window.addEventListener('unhandledrejection', function(err) {
    Raven.captureMessage(err.reason);
 });

or would you recommend perhaps Raven.captureMessage('unhandled rejection', err.reason)? I thought that might result in every uncaught promise being grouped together mistakenly as the same problem.

There is some new fancy stacktrace generation for messages (thought I dont know much about it), and when that’s present we’d use it for grouping.

If thats not present however, you may want to add fingerprints:

Raven.capturemessage('Unhandled Rejection', {fingerprint: [err.reason, '{{ default }}']})

Alternatively, just add err.reason to the message string, which likely will increase usability.

I like your idea to add the err.reason string to 'Unhandled Rejection' - I think that would work best in the web UI. thanks