Promise.catch (): how to distinguish between operational deviations and software throws

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 => {
        // all rejects and throws end up here
        console.log(error);
    })
})

function selectFromDbByEmailAndCheckPwAndReturnId (email, password) {
   return new Promise((resolve, reject) => {
      db.sql.query( /* params */, (error, results) => {
          blarg = 1; // <-- reference error, programmatic
          // do your SELECT * FROM Users where email=? ... etc.
          if (error) {
               return reject(error); // <-- operational sql error
          :
          :
          if (resultsRowsFromQuery.length === 0) {
             // vvvvv operational error: user not found
             return reject(new Error("User not in database"));
          }
          :
          // hash password & salt, etc etc etc ...
          :
          return resolve(resultRowsFromQuery[0].id);
      });
   });
}
// no need to code out updateUserIdLoginToken...

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;
      }
      // no error, got id, do next step...
      updateUserIdLoginToken(id, login_token, error => { 
         // do next thing, like return res.render() or something...
      });
    });
  } catch (e) {
    console.error("Programmatic error:", e);
  }
})

function selectFromDbByEmailAndCheckPwAndReturnId (email, password, callback) {
  db.sql.query( /* params */, (error, results) => {
      blarg = 1; // <-- reference error, programmatic
      // do your SELECT * FROM Users where email=? ... etc.
      if (error) {
         return callback(error, null);
      }
      :
      :
      if (resultsRowsFromQuery.length === 0) {
         // vvvvv operational error: user not found
         return callback(new Error("User not in database"), null);
      }
      :
      // hash password & salt, etc etc etc ...
      :
      return callback(null, id);
  });
}
+4
4

node - . , / -, . , . node - , , ( try-catch, if(error)). . " " , .

node - , , . , .

, , ? , , :

+3

, . . , , , - , , "" ... catch. , - . , , promises , , . , , , , , .

:

function logMeIn (email, password, login_token) {
    selectFromDbByEmailAndCheckPwAndReturnId (email, password)
    .then(response => { 
      if(response.error) {
        // Operational error
      } else {
        // successful response
      }
    })
    .catch(error => {
        // programmatic errors;
        console.log(error);
    })
})

function selectFromDbByEmailAndCheckPwAndReturnId (email, password) {
   return new Promise((resolve, reject) => {
      db.sql.query( /* params */, (error, results) => {
          blarg = 1; // <-- reference error, programmatic
          // do your SELECT * FROM Users where email=? ... etc.
          if (error) {
               return resolve({ error }); // <-- operational sql error
          :
          :
          if (resultsRowsFromQuery.length === 0) {
             // vvvvv operational error: user not found
             return resolve({ error: new Error("User not in database") });
          }
          :
          // hash password & salt, etc etc etc ...
          :
          return resolve({ result: resultRowsFromQuery[0].id });
      });
   });
}
+2

, , , Promise .

, . ​​ , try/catch

  • Promise,
  • then .catch (onFulfilled orRejected),
  • then , ,

, then catch . .

, , .

try/catch , . , catch , , .

catch , .catch .

+2

Node JS . , "type" catch.

In the event of a programming error, the type value will be one of EvalError, RangeError, ReferenceError, SyntaxError, TypeErro, or URIError.

If you don’t want to do this manually, look at the bounce npm module.

0
source

Source: https://habr.com/ru/post/1694747/


All Articles