javascript Node.js 未处理的拒绝错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51317157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js Unhandled Rejection Error
提问by swigganicks
I'm getting an Unhandled Rejection error in my code but the trace won't tell me what's causing it. I think it's the webp.cwebpcall that is causing the issue. When I run the code, I successfully convert the image and log status and then run into the Unhandled Rejection. It seems like I don't enter into the last two .then(()blocks since no console messages get logged from them.
我的代码中出现 Unhandled Rejection 错误,但跟踪不会告诉我是什么原因造成的。我认为这webp.cwebp是导致问题的电话。当我运行代码时,我成功转换了图像和日志状态,然后遇到了未处理的拒绝。似乎我没有进入最后两个.then(()块,因为没有从它们记录控制台消息。
How can I properly handle the rejection error to avoid this error? I've tried inserting and removing statusin the resolve()and reject()statements but it doesn't seem to fix it.
如何正确处理拒绝错误以避免此错误?我试过status在resolve()andreject()语句中插入和删除,但它似乎没有修复它。
// Download image file from Google Cloud Storage bucket.
return file.download({ destination: tempLocalFilename })
.catch((err) => {
console.error('Failed to download file.', err);
return Promise.reject(err);
})
.then(() => {
console.log(`Image ${file.name} has been downloaded to ${tempLocalFilename}.`);
// Convert PNG to webp using webp-converter.
return new Promise( (resolve, reject) => {
webp.cwebp(tempLocalFilename, newLocalFilename, "-q 80", status => {
console.log(status);
if (status === '100') {
resolve();
} else {
reject(status);
}
}
);
});
})
.then(() => {
console.log(`Image ${file.name} has been converted.`);
// Upload the converted image back into the bucket.
return file.bucket.upload(newLocalFilename, { destination: file.name })
.catch((err) => {
console.error('Failed to upload converted image.', err);
return Promise.reject(err);
});
})
.then(() => {
console.log(`Converted image has been uploaded to ${file.name}`);
// Delete the temporary file.
return new Promise((resolve, reject) => {
fs.unlink(tempLocalFilename, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
回答by Samuel Toh
Q:I'm getting an Unhandled Rejection error, how can I properly handle the rejection error?
问:我收到未处理的拒绝错误,我该如何正确处理拒绝错误?
A:Like what the comments already said, a .catch(...)will stop your exception from bubbling up to become an unhandled rejection error.
A:就像评论已经说过的那样,a.catch(...)将阻止您的异常冒泡成为unhandled rejection error.
Alternatively you can also insert reject handler function for each of the .then(...)clause. That is, for each .then()it should take in 2 functions, one for the happy path and the other for the bad path.
或者,您也可以为每个.then(...)子句插入拒绝处理函数。也就是说,对于每个.then()函数,它应该有 2 个函数,一个用于快乐路径,另一个用于坏路径。
E.g.
例如
return new Promise( (resolve, reject) => {
webp.cwebp(tempLocalFilename, newLocalFilename, "-q 80", status => {
...
}
);
});
})
.then(/*happy path=*/() => {
console.log(`Image ${file.name} has been converted.`);
// Upload the converted image back into the bucket.
return file.bucket.upload(newLocalFilename, { destination: file.name })
.catch((err) => {
console.error('Failed to upload converted image.', err);
return Promise.reject(err);
});
},
/*unhappy path=*/ (error) => {
console.log("oops something went wrong during uploading");
})
/*catch all rejection=*/
.catch(error => {
console.log("something bad happened somewhere, rollback!");
});

