6
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?

More than 1 year has passed since last update.

UnhandledRejectionエラーを握りつぶす

Posted at

エラー内容

Vue.jsに設定したSentryから以下のエラーが飛んできました。
Image from Gyazo

UnhandledRejection
Non-Error promise rejection captured with value: cancel

エラーの意味

promiseでcancelが返ってきたが、ハンドルできなくて失敗したよ!と言っています。

エラーコードサンプル

Vue.jsのライブラリ「Element-Plus」の確認ダイアログで発生するサンプルです。表示された確認ダイアログで「いいえ」を選択したときは、.thenではなく.catchに遷移しますが、.catchが定義されていないためUnhandledRejectionが発生します。

  ElMessageBox.confirm(
    '削除しますか?',
    '注意',
    {
      confirmButtonText: 'はい',
      cancelButtonText: 'いいえ',
      type: 'warning'
    }
  ).then(() => {
    // ~~削除処理~~
  })

対処方法

.catch式を書けば発生しません

  ElMessageBox.confirm(
    '削除しますか?',
    '注意',
    {
      confirmButtonText: 'はい',
      cancelButtonText: 'いいえ',
      type: 'warning'
    }
  ).then(() => {
    // ~~削除処理~~
  }).catch((error) => {
    // UnhandldRejectionのためにとりあえず定義する。何も処理は書かなくてもOK
  })

以上です!

6
1
0

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
6
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?