Adonis has detected an unhandled promise rejection, which may cause undesired behavior in production

1.2k Views Asked by At

You are using adonisJS in a health check service and are trying to make two requests according to axes in a Service. I get a warning from Adonis "Adonis has detected an unhandled promise rejection, which may cause undesired behavior in production "and my scheduler that monitors this service every 3 minutes just for. What's wrong with my call and why does adonis complain about it?

My strategy was to make a post for a route without login and right after doing a get for a logged route. I take the request token from the post and play it in the request header, but adonis gives me this warning. What is it?

App/Services/JaiminhoService

   try {
       await axios.post(Env.get('JAIMINHO_URL'), data).then(response => {
         if(response.status === 200) {
           try {
            await axios.get(`${Env.get('JAIMINHO_URL')}/push/schedule/`, {
              headers: { Authorization: `Bearer ${response.data?.token}` }
            }).then(response => {
              if(response.status === 200) {
                return {
                  status: response.status,
                  message: response.statusText,
                  service_name: jaiminho,
                  date,
                }
              }
            })
           } catch (error) {
             return 'Error'
           }

          }
        else  {
          //send mail
        }
       })
       return
    } catch (error) {
      return {
        message: 'Error! Please check Jaiminho service.',
        service_name: jaiminho,
        date
      }
    }


Warning: Adonis has detected an unhandled promise rejection, which may cause undesired behavior in production. To stop this warning, use catch() on promises or wrap await calls inside try/catch.

1

There are 1 best solutions below

0
On

Since you are using async/await, you could avoid nesting the .then() calls.

I recommend you to change your code to something like this and see what happens:

try {
    const postResponse = await axios.post(Env.get('JAIMINHO_URL'), data);
    if (postResponse.status === 200) {
      const getResponse = await axios.get(`${Env.get('JAIMINHO_URL')}/push/schedule/`, {
      headers: { Authorization: `Bearer ${postResponse.data?.token}` }});
    if (getResponse.status === 200) {
      return {
        status: response.status,
        message: response.statusText,
        service_name: jaiminho,
        date
      };
    }
  } else {
    //send mail
  }


} catch (error) {
    return {
      message: 'Error! Please check Jaiminho service.',
      service_name: jaiminho,
      date
    };
}