在 catch 块中抛出错误 Q promise 不拒绝承诺
throw error in catch block Q promise not rejecting the promise
我有以下函数被另一个函数调用:
function fetchAllRecords(client, item, region, offset, savedCount, totalCount) {
offset = offset || 1;
savedCount = savedCount || 0;
totalCount = totalCount || 0;
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}).catch(function (err) {
if (err == 'done')
return 'done';
// All of the above steps have built-in retry so we assume there's a non-recoverable error in this batch and move to next
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}
else {
******************* Problem Line ********************
console.log('Miscellenious error in fetachAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
在另一个函数中也是这样调用的
function processResource(client, item, debugmode, region) {
var deferred = q.defer();
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
deferred.resolve(result);
})
.catch(function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
deferred.reject(err);
});
return deferred.promise;
}
在上面的问题行中,它应该拒绝 fetchAllRecords
并且在 processResource
中应该调用 fetachAllResources
catch
处理程序,但是由于一些奇怪的原因,问题行在 throw 之后一直被调用,并且在被调用了十几次(随机)之后,它最终拒绝了 fetchAllResources
在 processResource
中返回的承诺。
我在这里遗漏了一些明显的东西吗?也请评论一下我使用 promise 的风格,可以吗还是我需要更多练习?
我认为您收到的错误很可能发生在您对堆栈中的方法进行了十几次调用之后。
即,假设您在调用 processQueue 方法时遇到以下情况:
成功,成功,成功,其他失败
现在,观察我在下面的代码中标记的行。 (我将引用这些行,例如 LINE A2,它将在 fetchAllRecords 的第二次调用中引用 LINE A):
function fetchAllRecords(client, item, region, offset, savedCount, totalCount) {
offset = offset || 1;
savedCount = savedCount || 0;
totalCount = totalCount || 0;
/*********************** LINE A **************************/
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
/*********************** LINE B **************************/
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}).catch(function (err) {
if (err == 'done')
return 'done';
// All of the above steps have built-in retry so we assume there's a non-recoverable error in this batch and move to next
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}
else {
/*********************** LINE C **************************/
console.log('Miscellenious error in fetachAllRecords, moving to next resource');
console.log(JSON.stringify(err));
/*********************** LINE D **************************/
throw new Error('Misc');
}
});
}
当我们进入时发生的事情是:
- LINE A1 // 成功完成,通过 thens
- B1 行
- LINE A2 // 成功完成,通过 thens
- B2 行
- LINE A3 // 成功完成,继续
- B3 行
- LINE A4 // 错误...异常气泡
- LINE B3 // 异常正在冒泡...
- Q 服务 -> 在包含行 B3 的 then 块中检测到异常,重定向到 catch...假设这是一个杂项错误
- C3 行
- LINE D3 // 错误...
- Q 服务 -> 在包含行 D3 的 catch 块中检测到异常...
也就是说,在 B2 行创建的承诺被拒绝。
但是当返回这个 promise 时,它构成了方法 2 中线性 promise 链的一部分。因此这个拒绝被传播到 catch 上......所以我们接下来点击:
- C2 行
- LINE D2 // 错误...
- 重复。
- C1 行
- LINE D1 // 错误...
- Q 服务将拒绝传播回 processResource,然后到达那里的 catch 块。
这导致 D 行被调用的次数比进程资源中的 catch 块多。
希望这是有道理的。
RE 您对 promises 的使用 - 您在 processResource 方法中使用了反模式(参考:https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern ) - you should very rarely have to create deferreds yourself, instead, you can rely on the chaining behaviour of then and catch (see https://github.com/kriskowal/q)。即,您可以这样写:
function processResource(client, item, debugmode, region) {
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
return fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
return result;
})
.catch(function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
return Q.reject(err);
});
}
更笼统地说,如果你有转译器的能力,我建议使用像 babel(或打字稿)之类的东西——这意味着你可以用 ES6 箭头函数符号来编写,这可以做出很多承诺更具可读性。
您会收到大量日志,因为您使用了递归方法,并在每个级别上处理和重新抛出错误。同步写入,完全一样
function fetchAll(offset) {
if (offset > 5) throw new Error("message"); // let's say the inner one throws
try {
return fetchAll(offset+1);
} catch(e) {
console.error(e.message);
throw e;
}
}
fetchAll(0);
您应该也会在这里收到 5 条消息,对吗?
解决方法是不再处理来自内部结果的错误。要通过承诺实现这一点,请查看 difference between .then(…).catch(…)
and .then(…, …)
- 你想要后者:
function fetchAllRecords(client, item, region, offset=1, savedCount=0, totalCount=0) {
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}, function (err) {
// ^^
if (err == 'done')
return 'done';
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
} else {
console.log('Miscellenious error in fetchAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
function processResource(client, item, debugmode, region) {
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
return fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
return result;
}, function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
throw err;
});
}
我有以下函数被另一个函数调用:
function fetchAllRecords(client, item, region, offset, savedCount, totalCount) {
offset = offset || 1;
savedCount = savedCount || 0;
totalCount = totalCount || 0;
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}).catch(function (err) {
if (err == 'done')
return 'done';
// All of the above steps have built-in retry so we assume there's a non-recoverable error in this batch and move to next
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}
else {
******************* Problem Line ********************
console.log('Miscellenious error in fetachAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
在另一个函数中也是这样调用的
function processResource(client, item, debugmode, region) {
var deferred = q.defer();
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
deferred.resolve(result);
})
.catch(function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
deferred.reject(err);
});
return deferred.promise;
}
在上面的问题行中,它应该拒绝 fetchAllRecords
并且在 processResource
中应该调用 fetachAllResources
catch
处理程序,但是由于一些奇怪的原因,问题行在 throw 之后一直被调用,并且在被调用了十几次(随机)之后,它最终拒绝了 fetchAllResources
在 processResource
中返回的承诺。
我在这里遗漏了一些明显的东西吗?也请评论一下我使用 promise 的风格,可以吗还是我需要更多练习?
我认为您收到的错误很可能发生在您对堆栈中的方法进行了十几次调用之后。
即,假设您在调用 processQueue 方法时遇到以下情况:
成功,成功,成功,其他失败
现在,观察我在下面的代码中标记的行。 (我将引用这些行,例如 LINE A2,它将在 fetchAllRecords 的第二次调用中引用 LINE A):
function fetchAllRecords(client, item, region, offset, savedCount, totalCount) {
offset = offset || 1;
savedCount = savedCount || 0;
totalCount = totalCount || 0;
/*********************** LINE A **************************/
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
/*********************** LINE B **************************/
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}).catch(function (err) {
if (err == 'done')
return 'done';
// All of the above steps have built-in retry so we assume there's a non-recoverable error in this batch and move to next
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}
else {
/*********************** LINE C **************************/
console.log('Miscellenious error in fetachAllRecords, moving to next resource');
console.log(JSON.stringify(err));
/*********************** LINE D **************************/
throw new Error('Misc');
}
});
}
当我们进入时发生的事情是:
- LINE A1 // 成功完成,通过 thens
- B1 行
- LINE A2 // 成功完成,通过 thens
- B2 行
- LINE A3 // 成功完成,继续
- B3 行
- LINE A4 // 错误...异常气泡
- LINE B3 // 异常正在冒泡...
- Q 服务 -> 在包含行 B3 的 then 块中检测到异常,重定向到 catch...假设这是一个杂项错误
- C3 行
- LINE D3 // 错误...
- Q 服务 -> 在包含行 D3 的 catch 块中检测到异常... 也就是说,在 B2 行创建的承诺被拒绝。 但是当返回这个 promise 时,它构成了方法 2 中线性 promise 链的一部分。因此这个拒绝被传播到 catch 上......所以我们接下来点击:
- C2 行
- LINE D2 // 错误...
- 重复。
- C1 行
- LINE D1 // 错误...
- Q 服务将拒绝传播回 processResource,然后到达那里的 catch 块。
这导致 D 行被调用的次数比进程资源中的 catch 块多。
希望这是有道理的。
RE 您对 promises 的使用 - 您在 processResource 方法中使用了反模式(参考:https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern ) - you should very rarely have to create deferreds yourself, instead, you can rely on the chaining behaviour of then and catch (see https://github.com/kriskowal/q)。即,您可以这样写:
function processResource(client, item, debugmode, region) {
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
return fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
return result;
})
.catch(function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
return Q.reject(err);
});
}
更笼统地说,如果你有转译器的能力,我建议使用像 babel(或打字稿)之类的东西——这意味着你可以用 ES6 箭头函数符号来编写,这可以做出很多承诺更具可读性。
您会收到大量日志,因为您使用了递归方法,并在每个级别上处理和重新抛出错误。同步写入,完全一样
function fetchAll(offset) {
if (offset > 5) throw new Error("message"); // let's say the inner one throws
try {
return fetchAll(offset+1);
} catch(e) {
console.error(e.message);
throw e;
}
}
fetchAll(0);
您应该也会在这里收到 5 条消息,对吗?
解决方法是不再处理来自内部结果的错误。要通过承诺实现这一点,请查看 difference between .then(…).catch(…)
and .then(…, …)
- 你想要后者:
function fetchAllRecords(client, item, region, offset=1, savedCount=0, totalCount=0) {
return processQueue(client, item, offset, region).then(function (result) {
return associate(result, item, region)
}).then(function (success) {
return saveBatch(item, success.allResources, region);
}).then(function (result) {
savedCount += result.savedCount;
totalCount += result.totalCount;
if (debugmode) {
console.log(savedCount + '/' + totalCount);
}
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
}, function (err) {
// ^^
if (err == 'done')
return 'done';
if (err.message === 'LIMIT_EXCEEDED') {
offset += item.limit;
return fetchAllRecords(client, item, region, offset, savedCount, totalCount);
} else {
console.log('Miscellenious error in fetchAllRecords, moving to next resource');
console.log(JSON.stringify(err));
throw new Error('Misc');
}
});
}
function processResource(client, item, debugmode, region) {
if (item.resource === "Property" && region === "ccmls") {
var cities = cities.list;
item.query = item.query + ' ,(City=|' + cities.join() + ')';
}
return fetchAllRecords(client, item, region)
.then(function (result) {
if (debugmode) {
console.log('fetchAllRecords: ' + item.resource + '.' + item.class + ' completed...');
}
return result;
}, function (err) {
console.log(item.resource + '.' + item.class + ' failed with error: ' + JSON.stringify(err));
throw err;
});
}