Promise Unhandled rejection null

112 views
Skip to first unread message

AndDM

unread,
Dec 16, 2015, 8:10:56 PM12/16/15
to nodejs
Hi, i've this code:

module.exports.run = function(event, context, cb) {
  myclass.queryAsync(...)
  .then(function (results) {
    if (results.field === "test") {
      throw "error";
    } else {
      return cb(null, "success");
    }
  })
  .catch(function (err) {
    return cb(err)
  });
};

for promise i'm using bluebirdjs and i've attached this code to a mocha test. If function give back a success, the test passes without problems. If the test has errors, the stdout give back: Unhandled rejection null, the mocha test goes on and then stops with exceeded timeout limit.

My mocha test:
  before("Description", function(done) {
    require('path').run(event.options, {}, function (err, results) {
      if (err) {
        throw err;
      }
      done();
    });
  });
  

Some suggestions about that?
Thanks for your time.
Best regards

Ebrahim Pasbani

unread,
Dec 16, 2015, 11:08:38 PM12/16/15
to nodejs
You shouldn't throw exception in a callback.
Instead throwing of that exception call
cb(error)

AndDM

unread,
Dec 18, 2015, 12:02:26 PM12/18/15
to nodejs
Hi, thanks for your reply, i've edit my code in:

module.exports.run = function(event, context, cb) {

  myclass.queryAsync(...)
 .then(function (results) {
   if (results.field === "test") {
     return cb("error");
   } else {
     return cb(null, "success");
   }
 })
 .catch(function (err) {
   return cb(err)
 });
};
The error is te same.
Best regards

Tim Davis

unread,
Dec 18, 2015, 12:02:45 PM12/18/15
to nodejs
If there is an error in your test, be sure to pass it to done(err) so that the test doesn't time out.

Tim Davis

unread,
Dec 19, 2015, 12:04:33 AM12/19/15
to nodejs
In your test, done has to be called to avoid timing out. In mocha if your unit under test causes an unexpected error, then you call done with that error so mocha can tell you. Here is a revision that calls `done` with the error.

before("Description", function(done) {
  require('path').run(event.options, {}, function (err, results) {
    if (err) {
      return done(err); // <- call done and avoid double callback
    }
    return done();
  });
});

Also, it is better to use real errors.

module.exports.run = function(event, context, cb) {
  myclass.queryAsync(...)
 .then(function (results) {
   if (results.field === "test") {
     return cb(new Error("error"));
   } else {
     return cb(null, "success");
   }
 })
 .catch(function (err) {
   return cb(err)
 });
};

AndDM

unread,
Dec 21, 2015, 9:43:43 AM12/21/15
to nodejs
Hi, thanks for your reply, i've modified my mocha test with this if:
if(err) done(err)
else done()
And now the error "Unhandled rejection null" not appears and the mocha test not exceeds the timeout.
Thanks for your time.
Best regards
Reply all
Reply to author
Forward
0 new messages