2017-03-17 194 views
1

我有一個LAMBDA_PROXY合併請求類型的API網關。在Lambda中調用context.succeed後,如預期的那樣(如下所示)將響應頭以代碼302發回。不過,我想處理500和404錯誤,我可以肯定,到目前爲止的唯一的事情,是我錯誤地返回錯誤,因爲我得到502錯誤的網關。我的情況有什麼問題?AWS API網關錯誤響應生成502「錯誤的網關」

這裏是我的handler.js

const handler = (event, context) => { 
    //event consists of hard coded values right now 
    getUrl(event.queryStringParameters) 
    .then((result) => { 
     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 
       } 
       }); 
     } 
    }); 
}; 

回答

4

如果拋出lambda函數(或context.fail)內的異常,API網關讀取它,就好像出了毛病與你的後端和回報502.如果這是您期望,並希望返回404分之500,使用context.succeed方法你想要的狀態代碼和消息的運行時異常:我有同樣的問題

if (parsed.error) { 
    let error = { 
    statusCode: 404, 
    headers: { "Content-Type": "text/plain" } // not sure here 
    body: new Error(parsed.error) 
} 
return context.succeed(error); 
+0

當我看到它,現在有這麼多的意義,謝謝 – jmcgui05

0

,在我的情況問題是我的功能是沒有返回任何東西context.done()。所以,而不是context.done(null),我做了context.done(null, {});