You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to track exceptions happening inside a service worker to Sentry. I have a very basic setting and everything seams to be fine, except that the error are never sent (or even tried to be send to sentry).
The code looks something like this:
self.importScripts('https://cdn.ravenjs.com/3.19.1/raven.min.js');Raven.config('sentryPublicKey').install();self.addEventListener('install',function(event){try{// some actions}catch(err){console.log(Raven.isSetup());Raven.captureException(err);}});
The console.log(Raven.isSetup()); line always print true, so Raven should be correctly set up at this point. And the line Raven.captureException(err); doesn't show any kind of error, but it doesn't do anything either. There's something that makes sentry not being able to run inside a service worker? Some one knows if there is a way to achieve this?
Thanks a lot!
Reacted by Nimish Singhal, Stephen Pinkerton and Sibelius Seraphini
To catch an errors themselves, use onerror or error event inside your ServiceWorker self.onerror = ... or self.addEventListener('error', ...), and when it happens, send a message to the parent (your code) and trigger Raven.captureException or Raven.captureMessage with the data you get from the passed message.
Mmm but this will work only if the user has the page loaded, but there are situations when you want to track errors without the page being loaded (ex: an error when receiving or trying to show a push notifications). There are no way to track an error using a fetch (get or post) request?
@kamilogorek@coluccini It is working fine for me, I am able to push errors to sentry by modifying _makeRequest. If the approach is okay and apart from my use case it is a general issue, I will be more than happy to create a PR and move XHR API's to fetch(https://github.com/github/fetch)
Hi @kamilogorek! I've saw that you merged the change to use fetch instead xhr when available (👍), but I'm seeing the same behaviour as before: Raven.captureException() don't trigger any request to sentry nor it triggers an error when called from the service worker. Do you have any idea why this could be happening? Thanks!
Activity
kamilogorek commentedon Nov 13, 2017
Hey @coluccini, ServiceWorkers doesn't have an access to XHR APIs, therefore it cannot communicate with an outside world – https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
I'll look into how this can be handled soon.
nimishsinghal commentedon Nov 14, 2017
I am also having the same issue. Is there any general practice people follow for error reporting in SW?
kamilogorek commentedon Nov 14, 2017
@nimishsinghal You can use https://github.com/bevacqua/swivel (or implement it on your own using https://ponyfoo.com/articles/serviceworker-messagechannel-postmessage) to pass a message when an error occurs.
To catch an errors themselves, use
onerrororerrorevent inside your ServiceWorkerself.onerror = ...orself.addEventListener('error', ...), and when it happens, send a message to the parent (your code) and triggerRaven.captureExceptionorRaven.captureMessagewith the data you get from the passed message.coluccini commentedon Nov 14, 2017
Mmm but this will work only if the user has the page loaded, but there are situations when you want to track errors without the page being loaded (ex: an error when receiving or trying to show a push notifications). There are no way to track an error using a fetch (get or post) request?
kamilogorek commentedon Nov 14, 2017
There is, but you need to create payload/urls yourself, as it's outside of SDK scope – https://docs.sentry.io/clientdev/overview/
coluccini commentedon Nov 14, 2017
Thanks! I'll have a look.
Should I close this issue?
kamilogorek commentedon Nov 14, 2017
Let's leave it open. I'll try to create a proper doc/feature one day.
nimishsinghal commentedon Nov 15, 2017
Thanks @kamilogorek @coluccini will try to create custom payloads today
nimishsinghal commentedon Nov 16, 2017
Hey @kamilogorek,
If we just edit _makeRequest method (https://github.com/getsentry/raven-js/blob/master/src/raven.js#L1884) and use fetch instead of XHR APIs will raven-js work for service workers?
coluccini commentedon Nov 16, 2017
Yes, but you would leave tons of browser out of compatibility.
Maybe add an new method using
fetchthat we can use from a service worker?nimishsinghal commentedon Nov 16, 2017
Can we use https://github.com/github/fetch so that modern browsers use fetch implementation and use XHR APIs for fallback?
nimishsinghal commentedon Nov 16, 2017
@kamilogorek @coluccini It is working fine for me, I am able to push errors to sentry by modifying _makeRequest. If the approach is okay and apart from my use case it is a general issue, I will be more than happy to create a PR and move XHR API's to fetch(https://github.com/github/fetch)
kamilogorek commentedon Nov 16, 2017
There's already a PR for this #1115
I'll try to merge it before the end of the week.
Not really, it'll increase a total size of raven, which we want to mitigate.
coluccini commentedon Nov 16, 2017
👏👏
coluccini commentedon Dec 1, 2017
Hi @kamilogorek! I've saw that you merged the change to use
fetchinsteadxhrwhen available (👍), but I'm seeing the same behaviour as before:Raven.captureException()don't trigger any request to sentry nor it triggers an error when called from the service worker. Do you have any idea why this could be happening? Thanks!nimishsinghal commentedon Dec 1, 2017
@coluccini I was using the same commit to track service workers error. It seems to be working fine for me.
coluccini commentedon Dec 1, 2017
@nimishsinghal great! I’ll check my implementation then. Thanks!
kamilogorek commentedon Dec 13, 2017
Release
3.21.0includes Fetch support now \o/