Search code examples
javascriptnode.jserror-handlinges6-promise

unhandledRejection nodejs


I know there's quite a lot of posts about this error, most of them have the same answer but somehow I am still getting the warning.

I have read something like this In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning? but instead of on I use once because of the event listener leak but somehow I still see the warning sometimes

I do want to get ride of the warning or solve it since its saying deprecated in the future but not sure when.

At first when I first run I would get this first

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: myrejectionmessage

then after, I will get this error

UnhandledPromiseRejectionWarning: myrejectionmessage 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: 5)

this is my original code without what I tried with the posts I found, I am trying to get some files in aws s3 bucket but there is possible that the file in bucket does not exist

this function is to compare if there is file then compare the modified time, if the file does not exist reject

exports.compareObjectMT = (s3, Key, getFileStat) => {
    const s3GetParams = {
        Bucket: process.env.S3_BUCKET,
        Key,
    };

    return new Promise((res, rej) => {
        s3.getObject(s3GetParams, (err, data) => {
            if (err) rej('myrejecterror');
            if (data) {
                res(String(getFileStat.mtimeMs) === data.Metadata.mtimems);
            }
            res(false);
        });
    });
};

Thanks in advance for any suggestions

This is how I am using the function

exports.s3Put = async (path) => {
    try {
        fs.readFile(path, async (err, fileBinary) => {
            if (err) throw err;
            // console.log(data, 'data');
            const s3 = new AWS.S3();
            const Key = path.replace(process.env.WATCH_PATH, '');
            const getStat = await getFileStat(path);
            console.log(getStat, 'getstateeeeeeeeeeeeeeee');
            const compareObj = await compareObjectMT(s3, Key, getStat);
            console.log(compareObj, 'compareObj');
        });
    } catch (e) {
        console.log(e, 'errorrrrrrrrrrrrr');
    }
};

Solution

  • //calling compareObjectMT ,Your return value is a Promise Object either resolve/reject
    
    //s3, Key, getFileStat aruments value you are passing
    
    compareObjectMT(s3, Key, getFileStat).then((value)=>{do something}) 
                                         .catch((err)=>console.error(err))
    

    what you are doing similar to this..Your callback after reading File inside try catch..It wont catch reject error from await

    you mightput all await inside single try catch block

    exports.s3Put = async (path) => {
    try {
        fs.readFile(path, async (err, fileBinary) => {
            if (err) throw err;
            // console.log(data, 'data');
             try {
            const s3 = new AWS.S3();
            const Key = path.replace(process.env.WATCH_PATH, '');
            const getStat = await getFileStat(path);
            console.log(getStat, 'getstateeeeeeeeeeeeeeee');
            const compareObj = await compareObjectMT(s3, Key, getStat);
            console.log(compareObj, 'compareObj');
          }catch (e) {
        console.log(e, 'errorrrrrrrrrrrrr');
    }
        });
    } catch (e) {
        console.log(e, 'errorrrrrrrrrrrrr');
    }
    

    };

    enter image description here