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!
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
It is designed to be fully async; as a consequence, APIs such as synchronous XHR and localStorage can't be used inside a service worker.
I'll look into how this can be handled soon.
I am also having the same issue. Is there any general practice people follow for error reporting in SW?
@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 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?
There is, but you need to create payload/urls yourself, as it's outside of SDK scope – https://docs.sentry.io/clientdev/overview/
Thanks! I'll have a look.
Should I close this issue?
Let's leave it open. I'll try to create a proper doc/feature one day.
Thanks @kamilogorek @coluccini will try to create custom payloads today
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?
Yes, but you would leave tons of browser out of compatibility.
Maybe add an new method using fetch
that we can use from a service worker?
Can we use https://github.com/github/fetch so that modern browsers use fetch implementation and use XHR APIs for fallback?
@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)
There's already a PR for this https://github.com/getsentry/raven-js/pull/1115
I'll try to merge it before the end of the week.
Can we use https://github.com/github/fetch so that modern browsers use fetch implementation and use XHR APIs for fallback?
Not really, it'll increase a total size of raven, which we want to mitigate.
👏👏
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!
@coluccini I was using the same commit to track service workers error. It seems to be working fine for me.
@nimishsinghal great! I’ll check my implementation then. Thanks!
Release 3.21.0
includes Fetch support now \o/
Most helpful comment
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.