Skip to content

Instantly share code, notes, and snippets.

@prashanta
Last active July 30, 2017 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prashanta/89b82a737e8e2ea300682d1b8f8fce7d to your computer and use it in GitHub Desktop.
Save prashanta/89b82a737e8e2ea300682d1b8f8fce7d to your computer and use it in GitHub Desktop.
Filtered Exception Catching in Bluebird Promise
node_modules
var Promise = require('bluebird');
var ErrorA = require('./ErrorA');
exports.method_1 = function() {
return new Promise(function(resolve, reject){
console.log("In Class_A.Method_1, delaying for 2 sec");
Promise.delay(2000, this)
.then(function(){
console.log("Class_A.Method_1 resolved");
resolve();
});
});
};
exports.method_2 = function() {
return new Promise(function(resolve, reject){
console.log("In Class_A.Method_2, delaying for 2 sec");
Promise.delay(2000, this)
.then(function(){
console.log("Class_A.Method_2 resolved");
resolve();
});
});
};
exports.method_3 = function() {
return new Promise(function(resolve, reject){
console.log("In Class_A.Method_3, delaying for 2 sec");
Promise.delay(2000, this)
.then(function(){
console.log("Class_A.Method_3 resolved");
resolve();
});
});
};
exports.method_4_fail = function(){
return new Promise(function(resolve, reject){
console.log("In Class_A.Method_4_fail fail, delaying for 2 sec");
Promise.delay(2000, this)
.then(function(){
reject(new ErrorA("Custom message goes here"));
});
});
};
exports.failFileRead = function(){
return new Promise(function(resolve, reject){
fs.readFile('./somefile.txt', function(err, data){
if (err)
reject(err);
else
resolve();
});
});
};
//This is Error A
function ErrorA(message) {
this.name = "Error A";
this.code = "code_error_A";
this.message = message;
}
ErrorA.prototype = Object.create(Error.prototype);
module.exports = ErrorA;
//This is Error A
function ErrorB(message) {
this.name = "Error B";
this.code = "code_error_B";
this.message = message;
}
ErrorB.prototype = Object.create(Error.prototype);
module.exports = ErrorB;
var Class_A_Method_1 = require("./ClassA").method_1;
var Class_A_Method_2 = require("./ClassA").method_2;
var Class_A_Method_3 = require("./ClassA").method_3;
var Class_A_Method_4_fail = require("./ClassA").method_4_fail;
Class_A_Method_1()
.then(Class_A_Method_2)
.then(Class_A_Method_3)
.then(Class_A_Method_4_fail) // this will fail
.catch(function(error){
console.log(error);
});
var MethodA_Success = require("./MethodA").success;
var MethodB_Success = require("./MethodB").success;
var MethodA_Fail = require("./MethodA").fail;
var MethodB_Fail = require("./MethodB").fail;
var MethodB_FailFileRead = require("./MethodB").failFileRead;
var ErrorA = require("./ErrorA");
var ErrorB = require("./ErrorB");
MethodA_Fail()
//.then(MethodA_Fail)
.then(function(result){
console.log(result);
})
//.then(MethodB_Fail)
.then(MethodB_FailFileRead)
.catch(function(error){
console.log("Caught Error A");
console.log(error);
})
.then(MethodB_Success)
.then(function(result){
console.log(result);
})
// .catch(ErrorA, function(error){
// console.log("Caught Error A");
// console.log(error);
// })
// .catch(ErrorB, function(error){
// console.log("Caught Error B");
// console.log(error);
// })
.catch({code: 'ENOENT'}, function(error){
console.log("Caught Error B");
console.log(error);
})
.catch(function(error){
if(error instanceof ErrorA)
console.log("Error A caught");
else if(error instanceof ErrorB)
console.log("Error B caught");
console.log(error);
});
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ]
2 info using npm@3.10.9
3 info using node@v6.9.2
4 verbose stack Error: missing script: start
4 verbose stack at run (/usr/local/lib/node_modules/npm/lib/run-script.js:151:19)
4 verbose stack at /usr/local/lib/node_modules/npm/lib/run-script.js:61:5
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:356:5
4 verbose stack at checkBinReferences_ (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:320:45)
4 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:354:3)
4 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:124:5)
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:311:12
4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
4 verbose stack at tryToString (fs.js:455:3)
4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:12)
5 verbose cwd /Users/prashanta/dev/sandbox/89b82a737e8e2ea300682d1b8f8fce7d
6 error Darwin 16.4.0
7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
8 error node v6.9.2
9 error npm v3.10.9
10 error missing script: start
11 error If you need help, you may report this error at:
11 error <https://github.com/npm/npm/issues>
12 verbose exit [ 1, true ]
{
"name": "89b82a737e8e2ea300682d1b8f8fce7d",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/89b82a737e8e2ea300682d1b8f8fce7d.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/89b82a737e8e2ea300682d1b8f8fce7d"
},
"homepage": "https://gist.github.com/89b82a737e8e2ea300682d1b8f8fce7d",
"dependencies": {
"bluebird": "^3.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment