0
votes

I'm getting the following error inside a catch:

Blockquote UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originared either by throwing inside of an async function without a catch block, orby rejecting a promise which was not handled with .catch().

This is the code that invokes it:

http({
      method: 'post',
      url: config.uri,
      headers: headers,
      data: data
    }).then(function (response) {
      resolve(response.data);
    }).catch(function (error) {
      console.log(error.response.data);
      reject(error.response.data);
    });

The console.log call inside the catch reads: {status: 'ERROR', code: 'EMAIL_FAIL', message: 'Email fail to send' }

This code that calls the function:

router.post('/declaration', csrf, async function (req, res, next) {
    let reqId = generateReqId();
    const ref = reqId.split('-')[0];
   let data = buildSubmission(fakeSub, res.locals.locale.toUpperCase(), ref);
    let headers = { ref: data.ref, reqId: reqId };
    const response = await callAPI.submitApplication(data, headers);

    casaApp.endSession(req).then(() => {
      res.status(302).render('../views/pages/confirmation.njk');
    }).catch((err) => {
      console.log(err);
      res.status(302).render('../views/pages/exit-error.njk');
    });
  });

Why am I getting this? I want the code to return to the calling segment which will handle the problem. Any suggestions?

1
Please show us more code. resolve and reject are undefined in your snippet...AKX
You are not catching the rejected promise when you call your async function.Reda Meskali
Have added the calling function!jenkinz
Catch is there for rejection, why are you calling reject() inside it. Please remove that.Plus that Reject is not defined anywhere.devd
Where are those resolve and reject functions that you're calling with your http method? If they don't exist, just simply do return response.data` inside the then callback and throw error.response.data inside the catch callback. You shouldn't be calling functions that are not defined. Additionally, make sure you're handling all your errors properly - use a try/catch like in the answer below and put everything in it.goto1

1 Answers

0
votes

Your not catch errors from callAPI.submitApplication request:

router.post('/declaration', csrf, async function (req, res, next) {
    const reqId = generateReqId();
    const ref = reqId.split('-')[0];
    const data = buildSubmission(fakeSub, res.locals.locale.toUpperCase(), ref);
    const headers = { ref: data.ref, reqId: reqId };
    try {
      const response = await callAPI.submitApplication(data, headers);
      await casaApp.endSession(req);
      res.status(302).render('../views/pages/confirmation.njk');
    } catch(e) {
      console.log(err);
      res.status(302).render('../views/pages/exit-error.njk');
    }
  });