getsentry / sentry-unity

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

WebGL JavaScript & native error handling #668

Open vaind opened 2 years ago

vaind commented 2 years ago

After the dotnet WebGL support is merged in #657, we can add support for javascript errors. There are two main approaches, the selection of the best one is part of this issue:

  1. either include sentry-javascript and let it do the reporting
  2. or implement the error handlers same as sentry-javascript does but forward errors to dotnet via wasm calls (if that's even possible - there's a list of all native functions available in development build sources)

In both cases, we would need to add an editor script that updates the generated JS sources (that supports both development & release builds, the latter including compression).

There's already a button to trigger a JS error - look for ThrowJavaScript(), enable it in NativeSupportScene.cs - but there would likely need to be more, e.g. to also include it in the SmokeTest.

As a related topic, we should check whether the selected approach also covers errors in the native code that are unhandled - you can reproduce those within the "Native Samples" scene in the sample repo.

vaind commented 2 years ago

Additional off-topic TODO: warn/error out during build when "com.unity.modules.unitywebrequest": "1.0.0" package is disabled - otherwise Unity just throws a vague error: Cannot create web request without initializing the system that I couldn't get any useful resolution for by googling

It requires the build script to be present so sounds like a good time to do it in a single PR.

bruno-garcia commented 2 years ago

either include sentry-javascript and let it do the reporting

I think this is the preferred solution as it's our largest SDK and has a lot of development going on there.

vaind commented 2 years ago

I've tried to integrate the browser bundle.js and it kinda works, as in the event is reported to sentry.io, however, I'm not sure how to get any stack trace info, because the scripts are loaded as blobs, which are, in case of a release build, also compressed...

Maybe we'd need to create a custom integration that does the stack parsing.

unity-web.zip

vaind commented 2 years ago

OK, so the stack traces for the embedded code coming from Unity .jslib are pretty limited but I don't think there's much we can do, they're just not there on the error reported by the browser.

The integration could still be useful at least to get the error type/text + stack traces if they're coming from standard javascript files.

What we would need to do is:

  1. Add sentry-js bundle to the sentry-unity package (either in repo or in CI) - I guess we're fine with including only the minified version, regardless of what kind of app user builds: https://browser.sentry-cdn.com/6.19.6/bundle.min.js
  2. Create a PostBuild integration to alter the generated index.html, adding the Sentry-javascript initialization to the beginning of <head>:
    <script src="Build/bundle.js"></script>
    <script>
      Sentry.init({
        dsn: "https://94677106febe46b88b9b9ae5efd18a00@o447951.ingest.sentry.io/5439417",
        release: "0.1.0",
        ....
      });
    </script>
  3. Make some kind of integration test: I think we could use SmokeTester.cs to launch the javascript error sample we already have in the example app. The existing WebGL python server would capture and check that.
  4. [optional] Implement scope sync - requires individual functions, similar to objective-c