title: Firebase Functions How To Handle Errors Properly
body: <h1>NOTE: this question is mainly about error handling, and if this is an ok approach, not about nesting promises, please read before closing</h1>
  
  <p>Since there are currently no error codes for services like firestore and firebase database, i'm using a system to know where the function failed and to handle error accordingly, simplified version below:</p>
  
  <pre><code>exports.doStuff = functions.https.onCall((data, context) =&gt; {
  
      return [promise doing stuff goes here].catch(error =&gt; { throw new Error('ERROR0') })
          .then(result =&gt; {
              return [promise doing stuff goes here, needs result of previous promise]
                  .catch(error =&gt; { throw new Error('ERROR1') })
          })
          .then(result =&gt; {
              return [promise doing stuff goes here, needs result of previous promise]
                  .catch(error =&gt; { throw new Error('ERROR2') })
          })
          .then(result =&gt; {
              //inform client function successful
              return {
                  success: true
              }
          })
          .catch(error =&gt; {
              if (error !== null) {
                  switch (error.message) {
                      case 'ERROR0':
                          //do stuff
                          throw new functions.https.HttpsError('unknown', 'ERROR0');
                      case 'ERROR1':
                          //do stuff
                          throw new functions.https.HttpsError('unknown', 'ERROR1');
                      case 'ERROR2':
                          //do stuff
                          throw new functions.https.HttpsError('unknown', 'ERROR2');
                      default:
                          console.error('uncaught error: ', error);
                          throw error;
                  }
              }
          });
  });
  </code></pre>
  
  <p>the thing is, for each <code>.catch()</code> inside each returned promise, i'm getting the following warning: <code>warning  Avoid nesting promises</code></p>
  
  <p>so my question is, is there a better way to handle errors?</p>