How to create a global error handler for async / wait es2017 javascript function?

Bluebird 3.4.1 for promises, Chrome 56, Babylon 6.23.1

Given:

  async login() {
    try {
      let response = await this.authservice.login('invalid-credentials');
    } catch (error) {

    }
  }

The above code mimics a 401 response error with a json object with a specific error message returned from the server that causes the catch statement. But instead of handling the error for each catch statement, I would like to have a global error handler.

In non-asynchronous functions, I use an event window.onerrorthat works as expected. However, this does not work for asynchronous functions.

I also added events for

window.addEventListener('unhandledrejection')
window.addEventListener('rejectionhandled')

these events do not fire for the above case (they fire for other cases). Instead, bluebird returns a "warning that the promise was rejected with an error: [object Response]".

this.authservice.login , .

, unhandledrejection, catch. , , .

, / promises.

+4

Source: https://habr.com/ru/post/1670790/


All Articles