javascript - Bluebird promises on Node.js: error and exception handling -


i use bluebird promises node.js application (mean environment). having trouble understanding exception/error handling though. consider following code:

var promise = require('bluebird'),     library1 = promise.promisifyall(require('firstlibrary')),     library2 = promise.promisifyall(require('secondlibrary'));  //main exports.urlchecker = function(req, res) {     library1.dosomething(req.body.url) //--> how reject promise?         .then(function(response) {             if (response == false) {                 throw new linkerror("url invalid!");             }              library2.foo(req.body.url)                 .then(function(response2) {                     if (response2 == false) {                         throw new sizeerror("url long!");                     }                     res.json({                         status: true                     });                 }).catch(linkerror, function(e) { //--> causes error!                     res.json({                         status: false                     });                 }).catch(sizeerror, function(e) { //--> causes error!                     res.json({                         status: false                     });                 }).catch(function(e) { //--> catch other exceptions!                     res.json({                         status: false                     });                 });         }); }; 

library1 - promisified:

exports.dosomething = function(url, cb) {     if (whatever == 0) {         cb(null, false); //--> return reject promise?     } else {         cb(null, true);     } }; 

i got 2 questions now.

  1. what have return library1 promise rejected? if not returning value, how can that?
  2. how define , catch own exceptions? code above results in error:

    unhandled rejection referenceerror: linkerror/sizeerror not defined 

.promisify*() turns regular node.js callback convention promises. part of convention errors passed first argument callback function.

in other words, reject promise, use cb(new error(...)).

example:

var promise = require('bluebird');  var library1 = {   // if `dofail` true, "return" error.   dosomething : function(dofail, cb) {     if (dofail) {       return cb(new error('i failed'));     } else {       return cb(null, 'hello world');     }   } };  var dosomething = promise.promisify(library1.dosomething);  // call resolves. dosomething(false).then(function(result) {   console.log('result:', result); }).catch(function(err) {   console.log('error:', err); });  // call rejects. dosomething(true).then(function(result) {   console.log('result:', result); }).catch(function(err) {   console.log('error:', err); }); 

as missing error types: assume exported secondlibrary use library2.linkerror instead of linkerror. if aren't exported cannot catch them explicitly.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -