Skip to content

unhandledRejection misfires a lot #6020

@ChALkeR

Description

@ChALkeR
import assert from 'node:assert'

globalThis.addEventListener('unhandledrejection', (e) => {
  console.error('unhandled rejection:', e.reason)
})

const asyncFunction = async (name) => {
  throw new Error(`this function rejects: ${name}`)
}

export const AssertBased = {
  async test(ctrl, env, ctx) {
    assert.rejects(async () => asyncFunction('A'))
  },
}

export const Manual = {
  async test(ctrl, env, ctx) {
    const error = await Promise.resolve().then(function () {
      return asyncFunction('B')
    }).then(function () {
      return null
    }).catch(function (e) {
      return e
    })

    // console.log('Got this error:', error)
  },
}

export const TryCatch = {
  async test(ctrl, env, ctx) {
    try {
      await Promise.resolve('C').then(asyncFunction)
    } catch (error) {
      // console.log('Got this error:', error)
    }
  },
}

In all these cases, A, B, and C are logged as unhandled rejections, while they are, in fact, handled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions