Lambdaでのこと。
長い通信をパスしたくて、axiosで下記のような書き方をした。
うおおおおおおお脳死try-catch!!!!!
try {
axios.post(requestUrl, params, {
headers: {
"Content-Type": "application/json",
"X-API-KEY": api_key,
}
})
.then(result => {
// ...
console.log(result.data);
})
}catch(e){
console.error('failed',e)
}
これでは、try-catchブロックは機能しませんね。
結果、axiosが失敗するとプロセス落ちして502行き。さよなら....。
UnhandledPromiseRejection
最近リリースされたNode.js 15ではデフォルトの設定が変更され、Unhandled Rejectionが発生した際にプロセスが強制終了されるようになりました。
https://zenn.dev/uhyo/articles/unhandled-rejection-understanding
await にしていない時点でtry-catchは機能しない。
下記のように修正する
axios.post(requestUrl, params, {
headers: {
"Content-Type": "application/json",
"X-API-KEY": api_key,
}
})
.then(result => {
// ...
console.log(result.data);
})
.catch(e => {
console.error('failed',e)
})
これでaxiosのエラーはcatchチェーンが受け止めてくれるようになった。
...そもそも長い処理はキューに入れましょう。AWSの意味ないやん。