forEach 未处理的拒绝错误
Unhandled rejection error with forEach
我试图在使用 forEach 迭代数组时处理拒绝。
这就是问题所在:
module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
return decrypt(config.slack_bot_token)
.then(function (auth_token) {
console.log('Dispatch got the token from kms');
subscribers.forEach(function(subscriber){
return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
});
})
.catch(function (error) {
console.error('Error during dispatch: ' + error);
throw new Error(error);
});
};
makePostCall
returns resolve
或 reject
并按照这些思路看起来...
function makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token) {
return new Promise(function (resolve, reject) {
const headers = ...
const url = ...
var payload = ...
request.post({
url: url,
headers: headers,
form: payload
}, function (error, response, body) {
if (!error && response.statusCode === 200 && JSON.parse(body).ok === true) {
console.log('successfully published to Slack');
return resolve();
}
else if (!error && response.statusCode === 200 && JSON.parse(body).ok === false) {
console.error('Status is 200 but ok is false: ' + JSON.stringify(body));
return reject(new Error(JSON.stringify(body)));
}
else {
console.error('Status other than 200: ' + JSON.stringify(error));
return reject(new Error(JSON.stringify(error)));
}
});
});
}
现在 运行 单元测试时,我遇到了很多未处理的拒绝错误
Unhandled rejection Error: {"status":404,"statusCode":404}
at Request._callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/lib/dispatchAlert.js:63:31)
at self.callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at OverriddenClientRequest.emit (events.js:188:7)
at /Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/nock/lib/request_overrider.js:212:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
作为参考,我使用 Bluebird for Promises。
我对 Node & Promises 很陌生。试图使用 async/await 来避免这种混乱,但 Node6.10 尚不支持。
其实,诺言无人倾听;他们只是被扔掉了。将承诺包装在 Promise.all()
中应该可以解决您的问题。
module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
return decrypt(config.slack_bot_token)
.then(function (auth_token) {
console.log('Dispatch got the token from kms');
return Promise.all(subscribers.map(function(subscriber){
return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
}));
})
.catch(function (error) {
console.error('Error during dispatch: ' + error);
throw new Error(error);
});
};
我试图在使用 forEach 迭代数组时处理拒绝。 这就是问题所在:
module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
return decrypt(config.slack_bot_token)
.then(function (auth_token) {
console.log('Dispatch got the token from kms');
subscribers.forEach(function(subscriber){
return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
});
})
.catch(function (error) {
console.error('Error during dispatch: ' + error);
throw new Error(error);
});
};
makePostCall
returns resolve
或 reject
并按照这些思路看起来...
function makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token) {
return new Promise(function (resolve, reject) {
const headers = ...
const url = ...
var payload = ...
request.post({
url: url,
headers: headers,
form: payload
}, function (error, response, body) {
if (!error && response.statusCode === 200 && JSON.parse(body).ok === true) {
console.log('successfully published to Slack');
return resolve();
}
else if (!error && response.statusCode === 200 && JSON.parse(body).ok === false) {
console.error('Status is 200 but ok is false: ' + JSON.stringify(body));
return reject(new Error(JSON.stringify(body)));
}
else {
console.error('Status other than 200: ' + JSON.stringify(error));
return reject(new Error(JSON.stringify(error)));
}
});
});
}
现在 运行 单元测试时,我遇到了很多未处理的拒绝错误
Unhandled rejection Error: {"status":404,"statusCode":404}
at Request._callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/lib/dispatchAlert.js:63:31)
at self.callback (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at OverriddenClientRequest.emit (events.js:188:7)
at /Users/alnu/IdeaProjects/meghaduta-alerter-slack-lambda/node_modules/nock/lib/request_overrider.js:212:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
作为参考,我使用 Bluebird for Promises。 我对 Node & Promises 很陌生。试图使用 async/await 来避免这种混乱,但 Node6.10 尚不支持。
其实,诺言无人倾听;他们只是被扔掉了。将承诺包装在 Promise.all()
中应该可以解决您的问题。
module.exports = function (alert, level, subscribers, tagSubscribers, message_type) {
return decrypt(config.slack_bot_token)
.then(function (auth_token) {
console.log('Dispatch got the token from kms');
return Promise.all(subscribers.map(function(subscriber){
return makePostCall(alert, level, subscriber, tagSubscribers, message_type, auth_token);
}));
})
.catch(function (error) {
console.error('Error during dispatch: ' + error);
throw new Error(error);
});
};