Получение UnhandledPromiseRejectionWarning несмотря на несколько блоков `try`-`catch` - PullRequest
0 голосов
/ 15 февраля 2019

Я использую сторонний модуль, который оборачивается вокруг их API.У меня есть следующий код:

const api = require('3rdpartyapi');

 async function callAPI(params) {
  try {
    let result = await api.call(params);
    return result;
  }
  catch(err) {
    throw err;  //will handle in other function
  }
 }
 
 async function doSomething() {
  try {
    //...do stuff
    let result = await callAPI({a:2,b:7});
    console.log(result);
  }
  catch(err) {
    console.error('oh no!', err);
  }
}

Несмотря на оба блока try - catch, сторонний API, когда он теряет соединение с homebase (случается довольно часто :(), взрывается с:

(node:13128) UnhandledPromiseRejectionWarning: FetchError: request to https://www.example.com failed, reason: getaddrinfo ENOTFOUND

Затем:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

Почему никто из моих try - catch не поймает это? Что именно не обрабатывается и как на самом деле с этим обращаться?

1 Ответ

0 голосов
/ 15 февраля 2019

Дело в том, что ожидание только преобразует отклонение «первого уровня» в ошибку, однако обещание может иметь обещание внутри, и их библиотека может не поймать отклонение внутри.Я сделал доказательство концепции 3rdpartyapi, которая может вызвать поведение, которое вы видите:


(async function () {

// mock 3rdpartyapi
var api = {
    call: async function(){
        await new Promise((resolve, reject) => {
            // why wrap a promise here? but i don't know
            new Promise((innerResolve, innerReject) => {
                innerReject('very sad'); // unfortunately this inner promise fail
                reject('this sadness can bubble up');
            })
        })
    }
};

// your original code
async function callAPI(params) {
    try {
        let result = await api.call(params);
        return result;
    } catch (err) {
        throw err; //will handle in other function
    }
}

async function doSomething() {
    try {
        //...do stuff
        let result = await callAPI({
            a: 2,
            b: 7
        });
        console.log(result);
    } catch (err) {
        console.error('oh no!', err);
    }
}

doSomething();

})();

Вывод:

$ node start.js
oh no! this sadness can bubble up
(node:17688) UnhandledPromiseRejectionWarning: very sad
(node:17688) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17688) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
...