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

183
Visualizações
JS error after setAttribute on SVG element is not catchable

I am working on a web tool, which lets you change attributes of SVG elements. Currently I am trying to give the user feedback on any invalid attributes or values.

When calling setAttribute (or setAttributeNS) on an SVG element with an unexpected value, a javascript error occurs. I tried to catch this by using try/catch or window.onerror, but could not find a way to do so.

Example code:

    <svg id="svg" width="300" height="100">
      <linearGradient id="gradient" x1="10" y1="10" x2="100" y2="100">
        <stop offset="0" stop-color="#000000"></stop>
        <stop offset="1" stop-color="#969695"></stop>
      </linearGradient>
    </svg>
    try {
       document.querySelector('#gradient').setAttribute('x1', 'This error is not catchable');
    } catch (error) {
       console.log('never triggered');
    }

https://jsfiddle.net/wr3tne2y/

tested in Chromium v91 and firefox:

js error slipping through try/catch

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You would need to check the value before setting it.

let val = 'This error is not catchable';
if (!isNaN(parseInt(val, 10)))
{
  document.querySelector('#gradient').setAttribute('x1', val);
}
else
{
  console.log('ERROR!');
}
about 4 years ago · Juan Pablo Isaza 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