title: AWS API Gateway error response generates 502 "Bad Gateway"
body: <p>I have an API Gateway with a LAMBDA_PROXY Integration Request Type. Upon calling <code>context.succeed</code> in the Lambda, the response header is sent back with code <code>302</code> as expected (shown below). However, I want to handle <code>500</code> and <code>404</code> errors, and the only thing I am sure about so far, is that I am returning the error incorrectly as I am getting <code>502 Bad Gateway</code>. What is wrong with my <code>context.fail</code>?</p>
  
  <p><strong>Here is my handler.js</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>const handler = (event, context) =&gt; { 
      //event consists of hard coded values right now
      getUrl(event.queryStringParameters)
      .then((result) =&gt; {
          const parsed = JSON.parse(result);
          let url;
          //handle error message returned in response
          if (parsed.error) {
              let error = {
                  statusCode: 404,
                  body: new Error(parsed.error)
              }
              return context.fail(error);
          } else {
              url = parsed.source || parsed.picture;
              return context.succeed({
                  statusCode: 302,
                  headers: {
                    Location : url
                  }
                });
          }
      });
  };
  </code></pre>