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

290
Visualizações
How to catch socket hang up error in node js

In Ukraine, we have tax API. It can sometimes be down, so it can be an error socket hang up. And I need to catch it because my app stops working. I've tried a lot, but it don't work.

import * as url from "url";
import * as http from "http";

export function query(method, toUrl, headers, payload, cb) {
    try {
        let parsed = url.parse(toUrl);
        let req = http.request({
            host: parsed.host,
            path: parsed.path,
            headers: headers,
            method: method,
        }, res => {
            let chunks = [];
            res.on("data", chunk => {
                chunks.push(chunk);
            });
            res.on("end", () => {
                cb(Buffer.concat(chunks));
            });
            res.on("error", () => {
                console.log("SOCKET ERR!!!");
                req.end();
                cb(null);
            });
        });
        req.on("error", e => {
            console.log("SOCKET ERR!!!");
            req.end();
            cb(null);
        });
        req.write(payload);
        req.end();
    }
    catch(e) {
        console.log("Error in query function: " + e.message);
    }
}

I also tried this:

process.on('uncaughtException', (err) => {
    console.log(err);
})

But it just stops. query function just don't catch errors. Only process.on catching error, but still it stops. I tried to like that:

process.on('uncaughtException', (err) => {
    console.log(err);
    startAsyncTasksFunction() // this is my main function
})

So, when there is uncaughtException, I just start main function. App don't stop and I have 2 startAsyncTasksFunction started and that's causing even more problems. So, how can I catch it in query function. I need this to make some manipulations when tax is not responding. And catch error in query function, not globally.

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