question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Capturing Exception from Service Worker

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilogorekcommented, 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

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.

1reaction
kamilogorekcommented, Nov 16, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does global error handling work in service workers?
The onerror property of the ServiceWorkerContainer interface is an event handler fired whenever an error event occurs in the associated service ...
Read more >
ServiceWorker: error event - Web APIs - MDN Web Docs
The error event fires whenever an error occurs in the service worker. Syntax. Use the event name in methods like addEventListener() , or...
Read more >
Error handling for failed network requests with service workers
Most service worker code samples show only the happy path where the fetched resource will eventually be available, eg: self.
Read more >
Service Worker updates and error handling with React - Medium
To address this issue, we need to update the registerValidSW function provided by Create React App in src/serviceWorker.js . Luckily we don't ...
Read more >
Service Workers and a Promise to Catch
You might, for example, find that you have a syntax error in /sw.js , which causes registration to fail. And if you do,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found