0

I am having problems do handle UnhandledPromiseRejectionWarning in a function that call other functions making axios request.

const getCode = async () => {
  const code = await baseURL.get('/gcs/login/k?codigoCanal=4')
  return code
}

const posLogin = async (code) => {
  const poslogin = await baseURL.get(`/resource/autenticar/${code}`)
  return poslogin
}

const login = async () => {
 try {
  const code = await getCode();
  const finishedLogin = async posLogin(code)
 } catch (error) {
   handleLoginError(error)
 }
}

const handleLoginError = (error) => {
  if (error.response.status === 400) {
      throw new Error(`Bad Request; ${error.response.statusText}`)
    }
  if (error.response.status === 401) {
    throw new Error(`Wrong credentials; ${error.response.statusText}`)
  }
  throw new Error('Unknown Error')
}


login()

Executing this code returns me UnhandledPromiseRejectionWarning, but I realized that if I put a try catch inside the catch of login functionn, this warning disappears

} catch (error) {
  try {
    handleLoginError(error)
  } catch (err) {
    console.log('Ooppss', err)
  }

When I do this my code enter in the nested catch and the "err" value is the value of the handleLoginError(error)

How can I do the same without executing a try catch inside the catch?

0

It seems you need to call login as follows:

try {
  await login();
}
catch(err) {
  // handle here the error
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.