Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda