Skip to content

Promise rejection with finally, does not throw unhandled rejection #47562

@sviat9440

Description

@sviat9440

Which @angular/* package(s) are the source of the bug?

zone.js

Is this a regression?

No

Description

Promise rejection with finally, does not throw unhandled rejection

Please provide a link to a minimal reproduction of the bug

https://github.com/sviat9440/zone-js-bug-reproduction.git

Please provide the exception or error you saw

Expected

  1. If the promise is rejected, the final callback should be executed
  2. After that, it should throw the UnhandledPromiseRejection error

Actual without zone.js

Promise.reject(new Error('test error')).finally(() => console.log('finally'));

Output:

finally
file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/work-as-expected.js:1
Promise.reject(new Error('test error')).finally(() => console.log('finally'));
               ^

Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/work-as-expected.js:1:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

Browser output:
image

Work as expected

Actual with zone.js

Case 1

import 'zone.js';

Promise.reject(new Error('test error')).finally(() => console.log('finally'));

Output:

finally

Process finished with exit code 0

Browser output:
image

Case 2

import 'zone.js';

Promise.reject(new Error('test error'));

Output:

Unhandled Promise rejection: test error ; Zone: <root> ; Task: null ; Value: Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/not-work-as-expected.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/not-work-as-expected.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 0

Browser output:
image

UnhandledPromiseRejection error is thrown only without finally callback, exit code was 0

Workaround

import 'zone.js';

try {
  await Promise.reject(new Error('test error'));
} finally {
  console.log('finally');
}

Output:

Unhandled Promise rejection: test error ; Zone: <root> ; Task: null ; Value: Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12) Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
finally
node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

Error: test error
    at file:///C:/Users/User/IdeaProjects/aap/zone-js-bug-reproduction/workaround.js:4:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Process finished with exit code 1

Browser output:
image

Error reported twice: UnhandledPromiseRejection and Error('test error'). Expected no UnhandledPromiseRejection from Zone

Side effect

Unhandled errors in promises with finally callback is missing. Console is empty. Logging systems also do not see such errors.

In browser the same situation.

Please provide the environment you discovered this bug in (run ng version)

"zone.js": "^0.11.8"

Anything else?

No response

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functionsarea: zonesIssues related to zone.jsstate: has PR

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions