title: Throwing exceptions on AWS Lambda with Node12.x in Axios catch handlers
body: <p>I'm having a hard time trying to understand how I can make this function work. I think it stems from me not really understanding 100% how to handle promises, but I've read all the tutorials I can find and I still don't see how I can do this.</p>
  <p>The following is a function, which calls an endpoint on an API. When it's successful, it'll return a token for use in future requests. When it fails, I want to throw an exception and completely stop the execution.</p>
  <p>I've heard that when throwing in a catch handler like this, then you're thrown exception is handled by a hidden try/catch and this passes to the next catch handler. But this is effectively meaning my exception gets black-holed. Which I've heard is a thing and I've read other questions on this website about that.</p>
  <p>The actual example isn't this trivial. But the effective code is still the same, a try/catch wrapped around all the code in question. But any exceptions never land in the right place. Then lambda fails with the exception in question. But it's not handled by the code that I wrote, so the response is wrong. I need to handle the code and output a different reply based on what exception is being thrown. So I change it around, fix up the data, add other related information and return that. But since I'm never landing in the right place. I can never do that.</p>
  <p>How could I alter this function, so that it correctly lands in the exception handler I've given? Thanks for your help</p>
  <pre><code>    let getToken = async () =&gt; {
          let transport = axios.create({ baseURL, headers: passwordHeaders });
  
          return transport.post('/authorization/login')
              .then(response =&gt; {
                  let accessToken = _.get(response, 'data.access_token', null);
                  if(accessToken !== null){
                      let base64 = Buffer.from(accessToken, 'base64');
                      let token = base64.toString('ascii');
  
                      return token.split(&quot;;&quot;).shift().split(&quot;:&quot;).pop();
                  }
  
                  throw new LoginError(response.statusText, response.status));
              })
              .catch(error =&gt; {
                  if(_.get(error, 'response.status', 'null') === 403){
                      throw new UnauthorizedError(error.response.statusText);
                  }
  
                  throw error;
              });
      }
  
      try{
          let token = await getToken();
      }catch(error){
          console.log(&quot;this exception should land here&quot;);
      }
  </code></pre>