2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UnhandledPromiseRejectionにご注意

Posted at

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の意味ないやん。

2
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?