unhandledRejection nodejs












0














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');
}
};









share|improve this question
























  • You need the consumer of compareObjectMT to catch errors.
    – CertainPerformance
    22 hours ago










  • @CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
    – Dora
    22 hours ago










  • Not with try/catch, but with .catch, the Promise method
    – CertainPerformance
    22 hours ago
















0














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');
}
};









share|improve this question
























  • You need the consumer of compareObjectMT to catch errors.
    – CertainPerformance
    22 hours ago










  • @CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
    – Dora
    22 hours ago










  • Not with try/catch, but with .catch, the Promise method
    – CertainPerformance
    22 hours ago














0












0








0







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');
}
};









share|improve this question















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');
}
};






javascript node.js error-handling es6-promise






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 21 hours ago

























asked 22 hours ago









Dora

1,42542349




1,42542349












  • You need the consumer of compareObjectMT to catch errors.
    – CertainPerformance
    22 hours ago










  • @CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
    – Dora
    22 hours ago










  • Not with try/catch, but with .catch, the Promise method
    – CertainPerformance
    22 hours ago


















  • You need the consumer of compareObjectMT to catch errors.
    – CertainPerformance
    22 hours ago










  • @CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
    – Dora
    22 hours ago










  • Not with try/catch, but with .catch, the Promise method
    – CertainPerformance
    22 hours ago
















You need the consumer of compareObjectMT to catch errors.
– CertainPerformance
22 hours ago




You need the consumer of compareObjectMT to catch errors.
– CertainPerformance
22 hours ago












@CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
– Dora
22 hours ago




@CertainPerformance I did try wrapping the inside of the function where const s3Get.....until the end of the function with try catch though
– Dora
22 hours ago












Not with try/catch, but with .catch, the Promise method
– CertainPerformance
22 hours ago




Not with try/catch, but with .catch, the Promise method
– CertainPerformance
22 hours ago












1 Answer
1






active

oldest

votes


















1














//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






share|improve this answer























  • I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
    – Dora
    21 hours ago










  • await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
    – BittuS
    21 hours ago












  • I edited my code to show how i actually used the compareObjectMT
    – Dora
    20 hours ago












  • const 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
    – BittuS
    20 hours ago










  • i have updated ,you can have a look
    – BittuS
    20 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941497%2funhandledrejection-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














//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






share|improve this answer























  • I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
    – Dora
    21 hours ago










  • await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
    – BittuS
    21 hours ago












  • I edited my code to show how i actually used the compareObjectMT
    – Dora
    20 hours ago












  • const 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
    – BittuS
    20 hours ago










  • i have updated ,you can have a look
    – BittuS
    20 hours ago
















1














//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






share|improve this answer























  • I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
    – Dora
    21 hours ago










  • await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
    – BittuS
    21 hours ago












  • I edited my code to show how i actually used the compareObjectMT
    – Dora
    20 hours ago












  • const 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
    – BittuS
    20 hours ago










  • i have updated ,you can have a look
    – BittuS
    20 hours ago














1












1








1






//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






share|improve this answer














//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







share|improve this answer














share|improve this answer



share|improve this answer








edited 19 hours ago

























answered 21 hours ago









BittuS

469112




469112












  • I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
    – Dora
    21 hours ago










  • await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
    – BittuS
    21 hours ago












  • I edited my code to show how i actually used the compareObjectMT
    – Dora
    20 hours ago












  • const 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
    – BittuS
    20 hours ago










  • i have updated ,you can have a look
    – BittuS
    20 hours ago


















  • I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
    – Dora
    21 hours ago










  • await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
    – BittuS
    21 hours ago












  • I edited my code to show how i actually used the compareObjectMT
    – Dora
    20 hours ago












  • const 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
    – BittuS
    20 hours ago










  • i have updated ,you can have a look
    – BittuS
    20 hours ago
















I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
– Dora
21 hours ago




I think I know why I keep on getting the warning now, because I wanted to use async and await and I kept on doing const co = await compareObjectMT(s3, Key, getFileStat) If I want to do it this way, is it possible?
– Dora
21 hours ago












await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
– BittuS
21 hours ago






await must be used inside async function, need to put try{const co = await compareObjectMT(s3, Key, getFileStat)}catch(e){}
– BittuS
21 hours ago














I edited my code to show how i actually used the compareObjectMT
– Dora
20 hours ago






I edited my code to show how i actually used the compareObjectMT
– Dora
20 hours ago














const 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
– BittuS
20 hours ago




const 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
– BittuS
20 hours ago












i have updated ,you can have a look
– BittuS
20 hours ago




i have updated ,you can have a look
– BittuS
20 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941497%2funhandledrejection-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas