Javascript Promise Pattern - differentiate error

后端 未结 3 1627
抹茶落季
抹茶落季 2020-12-11 11:03

Consider this block of code:

    getUser(userId)
    .catch(function(error){
        crashreporter.reportError(\'User DB failed\', error);
        // show us         


        
3条回答
  •  一整个雨季
    2020-12-11 11:40

    You should just have one catch for your chain, but you can add more context to the errors that are thrown in each function. For example, when an error condition occurs in chargeCreditCard you could tack onto the error a message property corresponding to what you want to report. Then in your catch error handler you can pass that message property into the reporter:

    getUser(userId)
      .then(chargeCreditCard)
      .catch(reportError);
    
    function reportError(error) {
      crashreporter.reportError(error.message, error);
    }
    

提交回复
热议问题