getsentry / sentry-unity

Development of Sentry SDK for Unity
https://docs.sentry.io/platforms/unity/
MIT License
208 stars 52 forks source link

`Unhandled` exceptions cause sessions to be marked as `crashed` #1721

Open WeiLingQiang opened 3 months ago

WeiLingQiang commented 3 months ago
          ![image](https://github.com/getsentry/sentry-unity/assets/103353466/270f6162-aae1-423f-84f4-1c585d905480)

There is an error: I throw an exception: throw new InvalidOperationException("xxx") It is considered a crashed, Shouldn't it be an errored ?

Originally posted by @WeiLingQiang in https://github.com/getsentry/sentry-unity/issues/1707#issuecomment-2220051502

WeiLingQiang commented 3 months ago
          ![image](https://github.com/getsentry/sentry-unity/assets/103353466/270f6162-aae1-423f-84f4-1c585d905480)

There is an error: I throw an exception: throw new InvalidOperationException("xxx") It is considered a crashed, Shouldn't it be an errored ?

Originally posted by @WeiLingQiang in #1707 (comment)

image

bitsandfoxes commented 3 months ago

Good catch! The SDK considers sessions that had unhandled exceptions happen as crashed as opposed to errored right now. But I get that within the context of a Unity game where Unity prevents the game from actually crashing based on an exception, this might be confusing.

WeiLingQiang commented 3 months ago

Thanks, What should I do if I want to count actual crashes in Unity games? And count the crash rate

WeiLingQiang commented 3 months ago

had unhandled exceptions happen

Because an unhandled exception occurs, the game will not actually crash

bitsandfoxes commented 3 months ago

What the SDK could do is to add to the mechanism type to basically filter exceptions. The only workaround for you, to unblock yourself, that I can see right now is to set the Exception.Data[Mechanism.HandledKey] = true in the BeforeSend callback.

bitsandfoxes commented 3 months ago

For what it's worth, we've had a very long discussion about resolving this (indirectly) in an RCF.

WeiLingQiang commented 3 months ago

Excuse me, is there a solution?

What the SDK could do is to add to the mechanism type to basically filter exceptions. The only workaround for you, to unblock yourself, that I can see right now is to set the Exception.Data[Mechanism.HandledKey] = true in the BeforeSend callback.

options.SetBeforeSend((sentryEvent, _) => {
    if (sentryEvent.Exception != null) {
        sentryEvent.Exception.SetSentryMechanism("error", "UnityException", true);
        sentryEvent.Exception.Data[Mechanism.HandledKey] = true;
    }
    return sentryEvent;
});

I set BeforeSend but it still doesn't work, help me !

WeiLingQiang commented 3 months ago

image

bitsandfoxes commented 3 months ago

Apologies for the delay. The mechanism is not the one on the actual exception-object but on the SentryException. Again, I don't recommend doing it that way but if you've got a valid usecase then it could look like this:

options.SetBeforeSend((sentryEvent, _) => {
    if (sentryEvent.Exception != null) {
        sentryEvent.SentryExceptions.FirstOrDefault().Mechanism.Handled = true;
    }
    return sentryEvent;
});
WeiLingQiang commented 3 months ago

Thanks, this helps me