How do I handle the UnhandledPromiseRejectionWarning in Node.JS - javascript

I have a few methods that use the request module to get images from URLs and returns it as a Promise, it works but when the image isn't found it rejects the promise with the status code 404. I looked into that warning and it is said that to handle the rejection you have to setup a catch callback after then() but I don't use then(), I use async/await.
This is the code to get the picture:
/**
* Returns picture from S3
* #param {String} filename Name of the file with extension
* #returns {String} Base64 string of the file
*/
getPictureFromS3: function (filename) {
return new Promise((resolve, reject) => {
let url = this.getURLFromS3(filename);
request(url, (err, res, body) => {
if (err) reject(err);
if (res.statusCode !== 200) {
reject(`Invalid status code <${res.statusCode}>`);
}
resolve(new Buffer.from(body).toString('base64'));
});
});
}
And this is how I call the method:
try{
socket.on('server get pictures', () => db.getPictures(data=>{
if(data!==null){
data.forEach(async e=>{
let picture = await utils.getPictureFromS3(e.getFilename());
});
}
}));
}catch(err){
console.log(err);
}
Full warning:
(node:256) UnhandledPromiseRejectionWarning: Invalid status code <404>
(node:256) 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: 1)
(node:256) [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.
Using Node v10.14.2.

There are two problems here
You care trying to do async operation inside forEach which doesn't work with promises. You need to use for..of loop or Promise.all if you can do things in parallel.
The exception is raise inside the listener so it will not bubble up outside so your catch wouldn't be executed. You need to move your try catch inside. Something like this
socket.on("server get pictures", () =>
db.getPictures(data => {
if (data !== null) {
for(const e of data) {
try {
let picture = await utils.getPictureFromS3(e.getFilename());
} catch (err) {
console.log(err);
}
}
}
})
);

Related

Unhandled promise rejection nodejs

I am trying to use openweather-apis to connect to a dialogflow agent. I am new to promises and I keep getting the warning UnhandledPromiseRejectionWarning and I'm not sure on how to fix this.
Currently I have 2 files weather.js, which makes the api call
const api = require("openweather-apis")
api.setAPPID(process.env.API_KEY)
api.setUnits("metric")
module.exports = {
setCity: function(city) {
api.setCity(city)
},
getWeather: function() {
return new Promise(function(resolve, reject) {
api.getTemperature(function(err, temp) {
if (err) reject(err)
resolve(temp)
})
})
}
}
And I make use of weatherinCity.js, which retrieves the city from the agent, calls the calling function and then sends a response to the user.
const weather = require("../../weather")
module.exports = {
fulfillment: function(agent) {
const city = agent.parameters.geo_city
weather.setCity(city)
weather.getWeather().then(function(temp) {
agent.add(
"It is ${temp} degrees Celcius in ${city}"
)
}).catch(() => {
console.error("Something went wrong")
})
}
}
full error message:
(node:2896) UnhandledPromiseRejectionWarning: Error: No responses defined for platform: DIALOGFLOW_CONSOLE
at V2Agent.sendResponses_ (C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\v2-agent.js:243:13)
at WebhookClient.send_ (C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:505:17)
at C:\Users\Coen\Desktop\ciphix-ca-case\node_modules\dialogflow-fulfillment\src\dialogflow-fulfillment.js:316:38
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2896) 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(). To terminate the node process on unhandled
promise rejection, use the CLI flag `--unhandled-rejections=strict` (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
(rejection id: 1)
(node:2896) [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.
This error indeed happened because this code fails to handle the Promise Rejection. While I'm not sure which Promise Rejection that failed to handle, but based on this and this GitHub discussions. It seems you need to return the agent.add() function.
I recommend trying async-await style with the consequence that you have to add a try catch block
module.exports = {
fulfillment: async function(agent) {
try {
const city = agent.parameters.geo_city
weather.setCity(city)
let temp = await weather.getWeather()
agent.add(
"It is ${temp} degrees Celcius in ${city}"
)
} catch (err) {
console.error("Something went wrong")
console.error(err)
}
}
}
Every error that is thrown on the try block should be caught in a catch block. Don't forget to add async before the function.
it will not solve your problem but generally speaking, i would add "return" after if(err). because otherwise the call to resolve would be done. in your particular case it will do no harm, as because of the nature of promises it will be ignored. but if you had written anything between reject and resolve it would have been executed.
// best practice
if (err) return reject(err)
for your problem, i've just tried this fast test to convice myself that even throws are catched by .catch() so i think you must be running a bad/old nodejs version, or the code you provided is not complete, and the failure is elsewere. I dont see any line pointing to your own code in the log O_o (only node_modules).
which nodejs version is it ?
var p = new Promise((resolve, reject) => {
throw new Error('test');
resolve('ok')
})
p.then(console.log).catch(function(err) {
console.error('err', err)
});

How to pass read data from await readFile to writeFile in fs module of node.js?

In this code, file is being opened and read successfully.
var objFs = require('fs')
async function openFile() {
await objFs.open('new.js', 'r', (argError, argFD) => {
if (argError)
throw -1
else
console.log("File opened!")
})
// This object is the promise.
const objReadResult = await objFs.readFile('new.js', (argError, argData) => {
if (argError)
throw 2
else
console.log('File read!')
})
await objFs.writeFile('new.js', (argError, objReadResult) => {
try
{
if( argError )
throw argError
else
console.log("File written")
}
catch( arg )
{
console.log(arg)
}
})
}
openFile()
What is the way to extract the data from await of readFile and pass it to writeFile?
The way I have written the writeFile function is producing the following error:
(node:21737) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
at maybeCallback (fs.js:145:9)
at Object.writeFile (fs.js:1332:14)
at openFile (/home/sulakshana/Documents/nodejs/programs/async_await.js:29:14)
(node:21737) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21737) [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.
File opened!
File read!
Few issues I can see:
there is a mix of syntaxes - async/await while also providing callbacks to the fs calls;
no need to call fs.open before fs.readFile;
there are no data to be written in fs.writeFile [docs];
there should be await-ing at the end;
and the final (the actual error reported) - there should be try...catch block to catch and react to any errors that occur.
Example implementation:
const fs = require('fs').promises;
async function openFile() {
const objReadResult = await fs.readFile('new.js');
console.log('File read!');
await fs.writeFile('new.js', /* PROVIDE DATA TO WRITE */);
console.log('File written');
}
(async () => {
try {
await openFile();
console.log('Program is done');
} catch (err) {
console.error(err);
}
})();

UnhandledPromiseRejectionWarning when using .reduce

const sendData = (response, language, locale) => {
try {
console.log(response.reduce((prev, curr) => prev + curr.confirmed, 0));
} catch (error) {
console.error('error');
}
};
and my fetch function:
const fetchGeneralData = async (param) => {
try {
let res = await axios.get(
`https://localhost/api/${param}`,
);
msg.reply(sendData(res.data.results), language, momentLocale);
} catch (error) {
msg.reply(language.errorMessage);
console.error(error, 'Error on fetchGeneralData');
}
};
The console.log shows me the correct value but for some reason, I still getting the errors.
I have tried adding async/await inside sendData but it did not work. My fetchGeneralData func works fine when i'm trying to return the date without modify it.
Here is the full message:
(node:5500) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: t.match is not a function
(node:5500) 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: 1)
(node:5500) [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.

Unexpected end of JSON input error after pending promise

I am in the process of creating a data handler and as part of that I need to download a file to then parse and work with as part of the handler.
I have wrapped the request for the file in a promise and asynchronously called the method from other methods
I have included the handler from a separate test file as follows
let DataHandler = require('./handler');
const handler = new DataHandler();
console.log(handler.getSupervisors());
The getSupervisors method is as follows
async getSupervisors() {
const parsed = await this.loadFile();
let supervisorList = [];
parsed.controllers.map(controller => {
if (controller.rating === 11 || controller.rating === 12){
supervisorList.push(controller);
}
});
return supervisorList;
}
This calls the loadFile method which simply calls the update method and before returning the json file required. The next 2 methods are update and downloadFile which I think are my 2 issues, they are as follows
async update() {
let body = await this.downloadFile();
const parsedJSON = JSON.parse(body);
parsedJSON.updated_date = new Date();
const json = JSON.stringify(parsedJSON);
fs.writeFile('vatsimData.json', json, function(err, result) {
if(err) console.log(err);
});
}
downloadFile() {
return new Promise((resolve, reject) => {
const urlList = [
'http://us.data.vatsim.net/vatsim-data.json',
'http://eu.data.vatsim.net/vatsim-data.json',
'http://apac.data.vatsim.net/vatsim-data.json'
];
const url = urlList[Math.floor(Math.random()*urlList.length)];
request(url, (error, response, body) => {
if (error) reject(error);
if (response.statusCode !== 200) {
reject('Invalid status code <' + response.statusCode + '>');
}
resolve(body);
});
});
}
The output of the getSupervisors method should be an array of all the 'supervisors' within the json file, instead it returns Promise { <pending> } and this error
(node:3160) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at DataHandler.loadFile (D:\Projects\VATSIM-Data-Handler\handler.js:57:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:3160) 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: 1)
(node:3160) [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 have tried putting the whole of the update method within a .then callback after calling downloadFile but it did not work
My loadFile method is as follows
async loadFile(){
await this.update();
return(JSON.parse(fs.readFileSync('vatsimData.json', {encoding:'utf-8'})));
}
The reason you are getting a promise is because, you are not awaiting the async function when you console.log(handler.getSupervisors());
you should do
handler.getSupervisors.then(val => console.log(val));
The error may be because the body response from the server may be malformed. try console.log body before calling JSON.parse() and use try catch to wrap and prevent unhandled errors
Update
From comments, you are trying to parse a file read with incorrect encoding
console.log(JSON.parse(fs.readFileSync('vatsimData.json',{encoding:'utf-8'})));
Add the encoding value in options.
Update
async update() {
let body = await this.downloadFile();
const parsedJSON = JSON.parse(body);
parsedJSON.updated_date = new Date();
const json = JSON.stringify(parsedJSON);
fs.writeFileSync('vatsimData.json', json, function(err, result) {
if(err) console.log(err);
}); // convert it to sync or resolve with a new promise when you complete the write to wait for the loadFile to have the file ready
}

UnhandledPromiseRejection in Express.js

I hope I'm supplying enough information for this question, but I can't understand why my callback function returns Unhandled Promise Rejection when I on purpose want to catch the error:
(node:3144) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Can't set headers after they are sent.
(node:3144) 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'm calling the function here in routes:
router.route("/home/create")
.post(Authorization, function(req, res) {
CreateSnippetResource(req, function(err) {
if (err) {
console.log(err.message)
}
res.redirect("/home")
});
});
And the "CreateSnippetResource"-function:
(function() {
let User = require("../../Models/User");
let Snippet = require("../../Models/Snippet");
/**
* Create a new snippet and save it to database
* #param request
* #param callback
*/
module.exports = function(request, callback) {
callback(
User.findOne({ user: request.session.Auth.username }, function(err, user) {
if (err || user === null) {
callback("User not found")
}
var snippet = new Snippet({
title: request.body.snippetName.split(".").shift(),
fileName: "." + request.body.snippetName.split(".").pop(),
postedBy: user._id,
snippet: [{
text: " "
}]
});
snippet.save().then().catch(function(err) {
callback(err)
});
}))
};
}());
I'm trying to handle the error when title is not entered. I have a validator in my schema-module that looks like this:
SnippetSchema.path("title").validate(function(title) {
return title.length > 0;
}, "The title is empty");
And indeed the returned error-message from the callback CreateSnippetResource is The title is empty. So how come I get this Promise-error?
I'm assuming it has something to do with how I handle the snippet.save(), but can't see how it's not handled. Can you please help?
Why does my callback function return Unhandled Promise Rejection when I on purpose want to catch the error?
That will happen when your callback throws another exception. This will reject the promise returned by the .catch(…) call, and that rejection is nowhere handled.
As it turns out, I was an idiot, and forgot that I accidentally putted the whole function in the callback. The callback is then executed twice, and thus returns Error: Can't set headers after they are sent.

Categories

Resources