sentry-javascript: How to pass extra data via captureException/captureMessage in latest sdk

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other:

Version:

4.0.6

Description

We can put a extra data in captureException / captureMessage when we use raven-js. Raven.captureException(err, extra)

How is that supported in latest sdk ?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 48
  • Comments: 28 (8 by maintainers)

Most upvoted comments

And the majority conclusion was that it’s much more flexible and easier to maintain this way.

Hey guys, this does look ridiculous. Did you see competitors’ API?

Rollbar.error("Something went wrong", e, {postId: 123}); //sending a message, exception and additional payload together
Rollbar.info("Post published", {postId: 123}); //sending a message with payload
Rollbar.error("Something went wrong"); //simple message is fine too

Yeah, I’ve sound the same solution, but indeed, it’s less sexy

            Sentry.withScope(scope => {
              scope.setExtra("state", state)
              scope.setExtra("data", data)
              Sentry.captureException(data.exception)
            });

Hello in 2019. It’s SO ANNOYING that I can’t just pass context as the second parameter in captureException. Please consider changing this.

If anyone came up with an elegant workaround/wrapping method please post!

For those looking from Google -> https://docs.sentry.io/platforms/javascript/enriching-error-data/additional-data/manage-context/#passing-context-directly

Looks like you can pass context keys like tags, extra, contexts, user, level, fingerprint, for example:

Sentry.captureException(new Error("something went wrong"), {
  tags: {
    section: "articles",
  }
});

@emmya that’s way more code than you need 😃

export const reportToSentry = ([error, context = {}]) => Sentry.withScope(scope => {
  scope.setTags(context.tags);
  scope.setExtras(context.extra);
  Sentry.captureException(error);
})

We handle string errors just fine unless you specifically want “message” level on purpose. You can also pass a whole object to setTags/setExtras and we’ll handle malformed/undefined input for you as well.

Only about extra data

I usage sentry 6.16.1

captureException(new Error('test 10'), { extra: { test: 1 } });

https://docs.sentry.io/platforms/javascript/guides/electron/enriching-events/context/

image

I think the new withScope is kind of pointless since you can’t use async functions with it, apparently. You pretty much have to have the withScope and captureException very close to each other and you know what would solve that? If captureException accepted a scope parameter! @kamilogorek

😐😐😐😐😐😐😐

docs

image

For a simple text message together with captureException use addBreadcrumb, for additional data use setContext (or breadcrumb data property):

function captureError(err, msg, data) {
    Sentry.captureException(err, scope => {
        if (msg)
            scope.addBreadcrumb({
                type: "error", // predefined types
                category: "error",
                level: Sentry.Severity.Error,
                message: msg
            });
        if (data)
            scope.setContext("extra-data", data);
    });
}

I don’t understand why there can’t be an overload for Sentry.captureMessage that also accepts an object for extra?

Now I have this in all my projects:

export function sentryMessage(message: string, extra?: object, skipLogging: boolean = false) {
  Sentry.withScope(scope => {
    if (extra) {
      for (const key in extra) {
        scope.setExtra(key, extra[key])
      }
    }

    scope.setLevel(Sentry.Severity.Info)
    Sentry.captureMessage(message)

    if (!skipLogging && (!env.SENTRY_URL || env.NODE_ENV === env.Environments.Test)) {
      logger.warn(message, extra)
    }
  })
}

export function sentryException(err: Error, extra?: object, skipLogging: boolean = false) {
  Sentry.withScope(scope => {
    if (extra) {
      for (const key in extra) {
        scope.setExtra(key, extra[key])
      }
    }

    scope.setLevel(Sentry.Severity.Error)
    Sentry.captureException(err)

    if (!skipLogging && (!env.SENTRY_URL || env.NODE_ENV === env.Environments.Test)) {
      logger.error(err.message, extra)
    }
  })
}

But this is a helper method that should be integrated into Sentry… I understand there are technical problems but surely you’ve noticed that this is the most asked question about Sentry.

This is bad for users that are upgrading and it’s bad for users that are new to Sentry. It’s good for developers of Sentry. I’m a developer so I get it, but I feel like Sentry may have lost sight of what is important on this specific issue.

We will add something like this, thanks all for the feedback! Will update the issue here again with the solution.

Ok, new to sentry - And the first think I learned is, that I can’t use Sentry for sending simple messages with an object as payload. That’s a pity since it’s a common usecase - What a about a extra function like "Sentry.captureMessage(‘crash-report’, { id : 2, …}); ?

@vincerubinetti using context gives you a better UI, as every key has its own section on the event page. extra is still supported for legacy reasons, so we still don’t deprecate it in this major version. It most likely will be the case for the next one though.

I literally just want to add a simple string message alongside the error, is that even doable ? like this (mentioned in the comment above) Rollbar.error("Something went wrong", e, {postId: 123});

In those doc links:

We allow the following context keys to be passed: tags, extra, contexts, user, level, fingerprint.

But then up the page a bit from that:

If you come across any usages of "extra" (setExtra in code) or "Additional Data" (in the UI), just mentally substitute it for context. Extra is deprecated in favor of context.

So what is the recommended approach?

Hey @jpbow ,

I just used the link you shared and its broken, maybe you wanted to refer to this link?

https://docs.sentry.io/platforms/javascript/enriching-error-data/additional-data/manage-context/

Thanks! Have updated my comment too