Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
How to hide stack trace from service worker fetch?

I have service worker like this:

self.addEventListener('fetch', function (event) {
    event.respondWith(new Promise(function(resolve, reject) {
       // ...
       fetch(event.request).then(resolve).catch(reject);
    }));
});

I don't remember where I found this code, but when I use ad blocker together with 404, in console I see stack trace:

Service worker Stack Trace in Dev console

Is there a way to get rid of that stack trace. I only need to know that the fetch failed (see the second error message).

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The stack trace is coming from the stack property of the Error object (or derived class) that is passed to the reject method. One solution is to reject with a string:

catch( err=>reject(err.message))

If, however, you want more information about the url that failed to fetch you would need to include it from information in event.request. How to include it depends on whether event.request is a url string, URL or Request object or object with a stringifier. Without exact information you could try something like

catch( err=>reject("Fetch failed to fetch " + (request?.url || request )));


 
about 4 years ago · Santiago Trujillo Report

0

Just clear the stack property:

const e = new Error('Uh oh');
console.log(e.name, e.message, e.stack);

e.stack = '';
console.log(e.name, e.message, e.stack);

about 4 years ago · Santiago Trujillo Report

0

It's seems that the proper way is to swallow the error, the network seems to get proper error that appear in Network and console tabs in Dev Tools.

self.addEventListener('fetch', function (event) {
    var req = fetch(event.request).then(resolve).catch(() => {});
    event.respondWith(req);
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!