Search code examples
javascriptnode.jsdialogflow-es

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.

Solution

  • 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.