unhandledRejection nodejs
//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');
}
};
Related videos on Youtube
Dora
learning to be a web developer. Just started so much to learn. Anyone need volunteers I need some work experience :P
Updated on June 04, 2022Comments
-
Dora almost 2 years
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 useonce
because of the event listener leak but somehow I still see the warning sometimesI 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 existthis 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'); } };
-
CertainPerformance over 5 yearsYou need the consumer of
compareObjectMT
to catch errors. -
Dora over 5 years@CertainPerformance I did try wrapping the inside of the function where
const s3Get
.....until the end of the function withtry catch though
-
CertainPerformance over 5 yearsNot with
try/catch
, but with.catch
, thePromise
method
-
-
Dora over 5 yearsI think I know why I keep on getting the warning now, because I wanted to use
async
andawait
and I kept on doingconst co = await compareObjectMT(s3, Key, getFileStat)
If I want to do it this way, is it possible? -
Biswadev over 5 yearsawait must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
-
Dora over 5 yearsI edited my code to show how i actually used the
compareObjectMT
-
Biswadev over 5 yearsconst compareObj = await compareObjectMT(s3, Key, getStat); for this you donot have any error handler,it wont get caught outside catch block,for that you have to throw error,for outside catch block you have added
-
Dora over 5 yearsThank you! I believe that worked like a charm. I always thought just one
try / catch
would be able to catch everything even if nested but this time it's because it's within anotherfs.readFile()
that's why it's not catching it if I understand correctly. I will just have to figure the rest out since somehow doesn't work as expected anymore :|