After much searching on Google, I cannot find a clear example of how to avoid programming every catch to find out if the Promise rejection error is software or operational. Compare this with the Node callback pattern to provide a callback (error, params ...), where operational errors are clearly indicated in the error parameter, and program errors are handled by throwing chains.
Please tell me that I am making a noob mistake, and there I missed the easy answer for that.
Consider a general example:
function logMeIn (email, password, login_token) {
selectFromDbByEmailAndCheckPwAndReturnId (email, password)
.then(id => { return updateUserIdLoginToken(id, login_token); })
.catch(error => {
console.log(error);
})
})
function selectFromDbByEmailAndCheckPwAndReturnId (email, password) {
return new Promise((resolve, reject) => {
db.sql.query( , (error, results) => {
blarg = 1;
if (error) {
return reject(error);
:
:
if (resultsRowsFromQuery.length === 0) {
return reject(new Error("User not in database"));
}
:
:
return resolve(resultRowsFromQuery[0].id);
});
});
}
catch , catch, , . , , , . (!)
sql.query, , , blarg=1 , .
, , . ( ()), , , .catch , .
, 7 bluebird, Q, A + ES6... ES6 Node/7/9... [ , .then(func A(), func B()). catch() B, catch(). LOL.]
?
№ 1: :
function logMeIn (email, password, login_token) {
try {
selectFromDbByEmailAndCheckPwAndReturnId (email, password, (error, id) => {
if (error) {
console.log("Operational error:", error)
return;
}
updateUserIdLoginToken(id, login_token, error => {
});
});
} catch (e) {
console.error("Programmatic error:", e);
}
})
function selectFromDbByEmailAndCheckPwAndReturnId (email, password, callback) {
db.sql.query( , (error, results) => {
blarg = 1;
if (error) {
return callback(error, null);
}
:
:
if (resultsRowsFromQuery.length === 0) {
return callback(new Error("User not in database"), null);
}
:
:
return callback(null, id);
});
}