title: How to catch errors thrown in node.js aws-sdk functions
body: <p>I'm using aws-sdk module in node.js, and am attempting to validate a config object which contains a profile parameter (sent as a string and used to construct a SharedIniFileCredentials object) and a region.</p>
  
  <p>To validate the sent region, I will create an ec2 object using a known region, and the profile parameter provided above and then check if the region paramter is found in the result of ec2.describeRegions.</p>
  
  <p>I'd like to validate the profile parameter by catching any exceptions thrown executing the above, and am trying this with the setup below:</p>
  
  <pre><code>var aws = require('aws-sdk'); // aws sdk
  var creds = new aws.SharedIniFileCredentials({profile: profile});
  var conf = new aws.Config({
      "credentials": creds,
      "region": "eu-west-1" //Known region
  });
  var ec2 = new aws.EC2(conf);
  try {
      ec2.describeRegions({}, function (err, data) {
          if (err) throw new Error(err.message);
          //Do stuff below to check if region is found in data.Regions array
          //...
          // If not found -&gt; errorCallback();
          successCallback();
      });
  } catch(err){
      errorCallback();
  }
  </code></pre>
  
  <p>It seems that when the profile is invalid, err.message is correctly coming out as:</p>
  
  <pre><code>CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
  </code></pre>
  
  <p>but instead of catching the error constructed from this, the error is unhandled and kills the process:</p>
  
  <blockquote>
    <p>C:\workspaces\njw\node_modules\aws-sdk\lib\request.js:31
                throw err;
                ^</p>
    
    <p>Error: Missing credentials in config, if using AWS_CONFIG_FILE, set
    AWS_SDK_LOAD_CONFIG=1
        at Response. (C:\workspaces\njw\test.js:69:24)
        at Request. (C:\workspaces\njw\node_modules\aws-sdk\lib\request.js:364:18)</p>
  </blockquote>
  
  <p>If I don't throw the error inside the describeRegions callback, it will be correctly handled by the catch clause. Any idea why this isn't working inside the callback?</p>