question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scope passed on `captureException` is not consistent on sentry dashboard

See original GitHub issue

Package + Version

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

Version:

^6.13.3

Description

I am capturing some errors and I try to add the scope like so:

function captureError({ severity = Severity.Error, error, failScope, extras }: CaptureErrorArgs) {
  withScope(function (scope) {
    scope.setTag('fail-scope', failScope);
    scope.setLevel(severity);
    extras && scope.setExtras(extras);
    captureException(error);
  });
}

Problem is that the errors on sentry dashboard have the scope sometimes and sometimes they dont.

I have tried all the different ways to pass the scope in the capture exception but none seems to work. Any help?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
onurtemizkancommented, Jun 14, 2022

Hi @paschalidi,

Errors caught by <ErrorBoundary /> are captured by Sentry SDK, and by design, an Error instance can only be captured once by Sentry to avoid duplication, as there are multiple levels of error handling inside SDK (details).

So, the Error instance passed to onError handler can’t be recaptured, as it’s already captured beforehand. Currently, there’s no way to skip SDK side capture for ErrorBoundary. captureMessage will work instead, because it’s not subject to duplicate error checks.

However, you can use beforeCapture handle to set tags / extras and such to errors, just before Sentry captures them.

The example below should modify the scope before the original captureException call on the SDK side, without creating a duplicate event.

class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = { counter: 0 };
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        this.setState(({ counter }) => ({
            counter: counter + 1,
        }));
    }

    render() {
        if (this.state.counter === 1) {
            // Simulate a JS error
            throw new Error("Hello");
        }

        return <button onClick={this.handleClick}>ErrorBoundary Error!</button>;
    }
}

export default Sentry.withErrorBoundary(Main, {
    fallback: function ErrorFallback() {
        return <div>Error</div>;
    },
    beforeCapture: function (scope) {
      return prepareScope(scope, 'foo', 'fatal', {bar: 'baz'});
    }
});

export function prepareScope(scope, failScope, severity, extras) {
    scope.setTag("fail-scope", failScope);
    scope.setLevel(severity);
    if (extras) {
        scope.setExtras(extras);
    }
    return scope;
}
1reaction
paschalidicommented, Jun 14, 2022

Hey @onurtemizkan thanks for explaining 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Capturing Errors | Sentry Documentation
Capture Exception. Open the views.py file. Notice that we import sentry_sdk lib which contains the capture_exception method:.
Read more >
React Error Handling And Reporting With Error Boundary And ...
With this setup, Sentry sends all errors captured by our error boundary to our issue dashboard using the Sentry.captureException method. Sentry ...
Read more >
JavaScript - Docs - Sentry Documentation
Start by capturing an exception: Sentry.captureException(new Error("Something broke"));. Then, you can see the error in your dashboard:.
Read more >
Track Errors in Ruby on Rails Application with Sentry
Project Link: https://github.com/Joker666/sentry-rails Error tracking is ... Unless we track these, we do not have full visibility into the ...
Read more >
getsentry/dotnet - Gitter
takes an argument there but IIRC the data goes as Extra and not as Tag. using(_logger.BeginScope(“my data”)) _logger.LogError(“uses Sentry without ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found