Skip to content

How to pass extra data via captureException/captureMessage in latest sdk #1607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 of 8 tasks
smilexu opened this issue Oct 1, 2018 · 28 comments
Closed
4 of 8 tasks

Comments

@smilexu
Copy link

smilexu commented Oct 1, 2018

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 ?

@Nabellaleen
Copy link

Nabellaleen commented Oct 1, 2018

Same problem here, and it's quite a bad news, because we're loosing all the information about current errors

And we can't go back to previous Raven version due to CDN issues ...

@ValkyrieUK
Copy link

ValkyrieUK commented Oct 1, 2018

Chaps, I've been running into the same issues. Check out this.
https://github.com/getsentry/sentry-javascript/tree/master/packages/node

Sentry.configureScope(scope => {
  scope.setExtra('battery', 0.7);
  scope.setTag('user_mode', 'admin');
  scope.setUser({ id: '4711' });
  // scope.clear();
});

Doesn't feel as nice as the Raven setup. I suggest wrapping Sentry in a client so you can set tags, userContext etc as you send the error.

@Nabellaleen
Copy link

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)
            });

@smilexu
Copy link
Author

smilexu commented Oct 1, 2018

setExtra might be the workaround, but you need to clear extra, that is annoying.

@kamilogorek
Copy link
Contributor

And we can't go back to previous Raven version due to CDN issues ...

@Nabellaleen what issues exactly?

Docs on scopes: https://docs.sentry.io/learn/scopes/?platform=browser

setExtra might be the workaround, but you need to clear extra, that is annoying.

You don't need if you use withScope.

I suggest wrapping Sentry in a client so you can set tags, userContext etc as you send the error.

You can still achieve this with mentioned withScope with ease.

There're a lot of reasons why we changed API. But it basically allows us to use event processors and handle states more efficiently in complex apps.

@smilexu
Copy link
Author

smilexu commented Oct 2, 2018

@kamilogorek thanks, I will try with withScope

@kamilogorek
Copy link
Contributor

I believe main issue was resolved. Feel free to let me know if it should be reopened.

@rhyek
Copy link

rhyek commented Dec 11, 2018

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

@kamilogorek
Copy link
Contributor

You can if you use promises the client directly, as we return async function from capture calls internally – #1449

If captureException accepted a scope parameter!

We've been there already and it was very hard to maintain and extend.

There're a lot of reasons why we changed API. But it basically allows us to use event processors and handle states more efficiently in complex apps.

@emmya
Copy link

emmya commented Oct 30, 2019

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!

@emmya
Copy link

emmya commented Oct 30, 2019

😐😐😐😐😐😐😐

docs

image

@kamilogorek
Copy link
Contributor

@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.

@Christilut
Copy link

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.

@kamilogorek
Copy link
Contributor

@Christilut we already discussed it plenty of times, externally and internally. And the majority conclusion was that it's much more flexible and easier to maintain this way. Without it, some integrations would simply not be possible. Not to mention that adding it now, would mean that we'd have to synchronize event data, scope data and additionally passed data. It'd just be too fragile. The way it's written is, yes, very verbose, I agree with that, but it's also easier to track where the ata comes from when necessary.

FWIW you don't have to iterate over keys yourself, as well as you don't have to set the levels, as those are defaults. This effectively makes your code way too longer than it should be. Here's a shorter, yet functionally equivalent snippet:

export function sentryMessage(message: string, extra?: object, skipLogging: boolean = false) {
  Sentry.withScope(scope => {
    scope.setExtras(extra)
    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 => {
    scope.setExtras(extra)
    Sentry.captureException(err)

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

@Christilut
Copy link

Thanks. I guess it's a necessary evil then, too bad but sometimes you don't have a choice :/

@LemyDanger
Copy link

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, ....}); ?

@artemv
Copy link

artemv commented May 13, 2020

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

@LemyDanger
Copy link

That's exactly what I'm looking for - Won't be hard to code, wont't it ;)

@HazAT
Copy link
Member

HazAT commented May 15, 2020

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

@jpbow
Copy link

jpbow commented Aug 20, 2020

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",
  }
});

@LeoBorai
Copy link

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/

@jpbow
Copy link

jpbow commented Sep 12, 2020

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

@vincerubinetti
Copy link

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?

@kamilogorek
Copy link
Contributor

@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.

@armouti
Copy link

armouti commented Jan 28, 2021

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});

@kamilogorek
Copy link
Contributor

@xmedeko
Copy link

xmedeko commented Apr 23, 2021

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);
    });
}

@AndreiSoroka
Copy link

AndreiSoroka commented Jan 10, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests