title: Error Handling in nested API Calls with express and nodeJS
body: <p>I have 3 microservices with an API interface with the goal to create a new user in my system.</p>
  <ol>
  <li>service1 --&gt; is the main service and will orchestrate the request along the other services</li>
  <li>service2 --&gt; will create the user in Firebase</li>
  <li>service3 --&gt; will create the user in the Data Base</li>
  </ol>
  <p>The frontEnd will call service1 and it will call first the service2 and then the service3. With the result, service1 will respond the frontEnd.</p>
  <p>If each service respond 200-OK everything works fine, the problem is the error handling.</p>
  <p>When service2 respond 401- &quot;User already exist in system&quot; to service1, I only can retrieve from error &quot;message: Request failed with status code 401&quot; and not the text &quot;User already exist in system&quot; sent by service2.</p>
  <p>How can I do for service1 will pass through the error code and error message sent by service2? 401- User already exist in system</p>
  <p><em>service1.js</em></p>
  <pre><code>app.post('/createAdminAgent', async (req, res) =&gt; {   
      let body = req.body;
      try {           
          const response_createAgentFirebase = await axios.post(service2URL + '/createAgent', req.body); //the user exist and will respond 401
          const response_createAgentBDD = await axios.post(service3URL + '/createEnterpriseAndAdminAgent', req.body);
          res.status(200).send(response_createAgentBDD.data);
      } catch (error) {           
          res.status(400).send(error); // how can I get here the text &quot;User already exist in system&quot; and the status code 401 sent by service2
            
  
      }                   
                      
  });
  </code></pre>
  <p><em>service2.js</em></p>
  <pre><code>app.post(&quot;/createAgent&quot;, async (req, res) =&gt; {  
      createAgentOnFirebase(req.body, async function(err, firebaseResult) {       
          if (err == null){
              res.status(200).send(firebaseResult);   
          }else{
              res.status(401).send({
                                   status: 401,
                                   error: 'User already exist in system'
                                   })
          }
      })
  })
  </code></pre>