0

The problem is that i am getting UNhandledPromiseRejection error eveen though i think i have handled all the cases. The code flows from profileRoutes to Controller to Utils where the error comes first.

Inside the profileRoutes.js

router.get('/:username', async (r, s) => {
    try{
        let profileData = await getProfileData(r.params.username);
        s.json({ success: true, payload: profileData });
    }catch(err){
        console.log('ending request processing by responding a error');
        s.status(500).json({ success: false, message: 'err[0].message' });
    }
});

Inside the controllers/index.js


const fetchQueue = [getUserRepos];
async function getProfileData(username) {
    let profileData = {};
    try{

        let results = await Promise.all(fetchQueue.map(item => item(username)));
        for (let i = 0; i < results.length; i++) {
            profileData[getKeys[i]] = results[i];
        }
        return profileData;

    }catch(err){
        console.log('error log in controller/index getProfileData function');
        throw err;
    }
}

const getUserRepos = async (username) => {
    
    try {
        // const res = await utils.gqlSender(username, 'userRepos', { createdAt });
        const res = await utils.gqlSender(username, 'userReposData');
        return res.user.repositories;
    } catch (err) {
        console.log('error log in controller/index getUserRepos function');
        throw err;
    }
};


Inside the utils/index.js


const gqlSender = async (username, type, opt = {}) => {
    
    axios.post('', {
        query: gqlQuery(username, type, opt) // generates a needed graphQL query
    }).then(res => {
        if(res.data.errors) {  // this is where the error is recieved and so i reject promise.                     
            console.log('bef@@@re'); 
            return Promise.reject (res.data.errors);
        }
        console.log('###',res.data);
        return res.data;
    }).catch(err => {
        console.log('error in making axios request inside utils/index gqlSender function');
        throw err;
        // return Promise.reject(err);
    });



The stack trace on making get request to /:username is-

error log in controller/index getUserRepos function
error log in controller/index getProfileData function
ending request processing by responding a error
bef@@@re
error in making axios request inside utils/index gqlSender function
(node:11260) UnhandledPromiseRejectionWarning: [object Array]
(node:11260) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:11260) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I dont think i am missing any Promise Rejection. Any help is appreciated. Thanks.

i have referred these answers previously -

pg07
  • 137
  • 2
  • 11

1 Answers1

0

Your gqlSender function is not returning the promise that will get rejected, so it is not handled anywhere. You should write either

const gqlSender = (username, type, opt = {}) => {
    return axios.post('', {
//  ^^^^^^
        query: gqlQuery(username, type, opt) // generates a needed graphQL query
    }).then(res => {
        if (res.data.errors) {
            console.log('error in making axios request inside utils/index gqlSender function');
            throw res.data.errors;
        } else {
            console.log('###',res.data);
            return res.data;
         }
    });
};

or

const gqlSender = async (username, type, opt = {}) => {
//                ^^^^^
    const res = await axios.post('', {
        query: gqlQuery(username, type, opt) // generates a needed graphQL query
    });
    if (res.data.errors) {
        console.log('error in making axios request inside utils/index gqlSender function');
        throw res.data.errors;
    } else {
        console.log('###',res.data);
        return res.data;
     }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375