Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

856
Vistas
Unable to handle net::ERR_CONNECTION_REFUSED in React

I am trying a post request to http://localhost:5000/run. If the server is not running then trying to throw "Please retry again" to client.

try{
    const {data} = await axios.post("http://localhost:5000/run",payload);
    setOutput(data.output);
}
catch ({ response }) {
  if (response) {
    const errMsg = response.data.err.stderr;
    setOutput(errMsg);
  } else {
    setOutput("Please retry submitting.");
  }
}

enter image description here

when the server is running proper error is received but not when the server is offline. Anything missing from the code?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Please try this code:

import axios from "axios";
import {AxiosError} from 'axios';

    axios
        .post("http://localhost:5000/run", payload)
        .then((response) => setOutput(response.data.output))
        .catch((error) => {
            if (!error?.response) {
                setOutput("No Server Response");
            } else if (error?.code === AxiosError.ERR_NETWORK) {
                setOutput("Network Error");
            } else if (error.response?.status === 404) {
                setOutput("404 - Not Found");
            } else if (error?.code) {
                setOutput("Code: " + error.code);
            } else {
                setOutput("Unknown Error");
            }
        });

You can use axios.isAxiosError(error) to make sure that the error is type of AxiosError.

Axios error types:

static readonly ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
static readonly ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
static readonly ERR_BAD_OPTION = "ERR_BAD_OPTION";
static readonly ERR_NETWORK = "ERR_NETWORK";
static readonly ERR_DEPRECATED = "ERR_DEPRECATED";
static readonly ERR_BAD_RESPONSE = "ERR_BAD_RESPONSE";
static readonly ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
static readonly ERR_CANCELED = "ERR_CANCELED";
static readonly ECONNABORTED = "ECONNABORTED";
static readonly ETIMEDOUT = "ETIMEDOUT";
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to try using response.response.data.error for displaying error in your case instead of response.data.err.stderr. So here you are getting error data from response :

try{
    const {data} = await axios.post("http://localhost:5000/run",payload);
    setOutput(data.output);
}
catch ({ response }) {
  if (response) {
    const errMsg = response.response.data.error;
    setOutput(errMsg);
  } else {
    setOutput("Please retry submitting.");
  }
}

Reference discussion here.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is the solution that worked . Thanks all for your inputs

try{
    const {data} = await axios.post("http://localhost:5000/run",payload);
    setOutput(data.output);
    }
    catch (error) {
      if (error.response.data) {
        setOutput(error.response.data.err.stderr);
      } 
      else {
        setOutput("Error connecting to server!");
      }
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda