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

125
Visualizações
How to be sure there is actually a file in a link before trying to createWriteStream

I want to be sure that there is actually a file to be downloaded before using fs.createWriteStream because as it is now, it downloads a 404 page and saves it as a xls file...

I have written the following code. How can I implement that and in case of failure I want to be able to send a message to slack

async function downloadFile() {
  function switchCase(value) {
    switch (value) {
      case '1':
        return 'jan';
      case '2':
        return 'fev';
      case '3':
        return 'mar';
      case '4':
        return 'abr';
      case '5':
        return 'mai';
      case '6':
        return 'jun';
      case '7':
        return 'jul';
      case '8':
        return 'ago';
      case '9':
        return 'set';
      case '10':
        return 'out';
      case '11':
        return 'nov';
      case '12':
        return 'dez';
      default:
        return;
    }
  }

  (async function (url, dest) {
    return new Promise((resolve, reject) => {
      var file = fs.createWriteStream(dest);

      var request = https
        .get(url, function (response) {
          response.pipe(file);

          file.on('finish', async function () {
            resolve(true);
            file.close();
            await closeDB();
          });
        })
        .on('error', async function (err) {
          fs.unlink(dest);
          reject(err);
          await fetch(
            `https://hooks.slack.com/services/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`,
            {
              method: 'post',
              body: JSON.stringify({
                text: `url: ${url}, dest: ${dest}, error: ${err}`,
              }),
              headers: { 'Content-Type': 'application/json' },
            }
          );
        });
    });
  })(
    `https://www.anbima.com.br/informacoes/merc-sec-debentures/arqs/d${
      new Date().getFullYear().toString().slice(2) +
      switchCase((new Date().getMonth() + 1).toString()) +
      (new Date().getDate() - 1).toString().padStart(2, '0')
    }.xls`,
    'd' +
      new Date().getFullYear().toString().slice(2) +
      (new Date().getMonth() + 1).toString() +
      (new Date().getDate() - 1).toString().padStart(2, '0') +
      '.xls'
  );
}
about 4 years ago · Juan Pablo Isaza
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