Skip to content
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

V3 Beta - errors not reported from preload context #372

Closed
3 tasks done
timfish opened this issue Oct 11, 2021 · 6 comments
Closed
3 tasks done

V3 Beta - errors not reported from preload context #372

timfish opened this issue Oct 11, 2021 · 6 comments

Comments

@timfish
Copy link
Collaborator

timfish commented Oct 11, 2021

Versions + Platform

Description

With v3 beta, errors are not reported from the preload context.

@timfish
Copy link
Collaborator Author

timfish commented Oct 11, 2021

I'm nowhere near a machine to test this but you can probably work around this by initialising the render code in the preload too:

import '@sentry/electron/preload';
Import { init } '@sentry/electron/renderer';

init();

If you're using context isolation, you will need to init the renderer code in the preload AND the isolated renderer context.

@mehrdadmms
Copy link

mehrdadmms commented Oct 13, 2021

Thank you. It's finally working.

For anyone who's looking at this issue in the future this is the code that worked for me.
I'm using Electron + Vue2.

Preload

require('@sentry/electron/preload');
const { init } = require('@sentry/electron/renderer');

init({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV,
  release: pJson.version
});

Main

import * as Sentry from '@sentry/electron/main';

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV,
  release: pJson.version
});

Renderer

import Vue from 'vue'
import * as Sentry from '@sentry/electron'
import * as SentryIntegrations from '@sentry/integrations'

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV,
  release: pJson.version,
  integrations: [
    new SentryIntegrations.Vue({
      Vue,
      logErrors: true,
      tracing: false,
    }),
  ],
})

@timfish
Copy link
Collaborator Author

timfish commented Oct 13, 2021

It's worth noting that as all events are sent from the main process, you do not need to supply the dsn, release and environment in the preload and renderer.

@timfish
Copy link
Collaborator Author

timfish commented Oct 31, 2021

I think reporting errors from the preload context should work out of the box by default but this adds some annoying complications that vary by the users Electron configuration.

For non-bundled apps, we automatically inject the preload script which is currently well under 1kB in size. If we want to support reporting errors from this context with zero config, we'll need to include all the renderer code which is currently ~78kB. If we include this code by default, we'll also need to call init and users will not be able to change the settings passed to init. In the preload context, passing options to init might not be a huge issue except...

If users have contextIsolation disabled (not the default and not recommended, but very common), the preload and renderer context are not distinct so once the SDK has been init'ed in the preload, it's effectively setup in the renderer and there is no way to pass option to init.

Why not let users just create their own preload script and choose if they want to support error reporting from this context and pass custom init options? Well, if sandbox mode is enabled (planned to become default), the preload has no access to require, so preload scripts will need to be bundled!

To summarise

  • If we init the renderer SDK in the preload, users with contextIsolation: false cannot pass options to init
  • If we don't init the renderer SDK in the preload, users that want errors from preload context will need to use a bundler if sandbox: true

@timfish
Copy link
Collaborator Author

timfish commented Nov 10, 2021

This is simpler in 3.0.0-beta.2

A preload is no longer required unless you're using Electron < v5 and bundling the main process.

If you would like errors reported from the preload then you'll still need to initialise the renderer portion of the sdk there. No options are required unless you need to configure specific renderer integrations.

preload.js

const { init } = require('@sentry/electron/renderer');
init();

If you have context isolation enabled, the renderer is a different context to the preload so you'll need to init the sdk again there.

@timfish timfish closed this as completed Nov 10, 2021
@its-tim-lee
Copy link

Hey, @timfish
So did you mean that, if I want to use Sentry in three independent contexts (ie., renderer, preload, main), and if i have enabled both contextIsolation and sandbox, as long as my Sentry configurations are the same in three contexts, i don't need to configure anything in init at the contexts of renderer and preload, since you metioned:

It's worth noting that as all events are sent from the main process, you do not need to supply the dsn, release and environment in the preload and renderer.

But I guess that I still need to configure the one in renderer, since that's the instruction from the relevant Sentry doc, that I
need to integrate @sentry/vue by doing something like:

  // this is in the renderer context
  Sentry = require("@sentry/electron/renderer")
  Sentry.init(
    /** my setting */,
    require("@sentry/vue").init
  )

Am i right?
Currently, I found that if I provide setting to the Sentry.init in the preload, I'll get an error, but the error will disappear if I don't provide any setting.

BTW,
I'm using @sentry/electron v4.20.0, @sentry/vue v7.105.0, and electron v29.1.1

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

3 participants