[英]Node.js Unhandled Rejection Error
I'm getting an Unhandled Rejection error in my code but the trace won't tell me what's causing it.我的代码中出现 Unhandled Rejection 错误,但跟踪不会告诉我是什么原因造成的。 I think it's the
webp.cwebp
call that is causing the issue.我认为是
webp.cwebp
调用导致了问题。 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.似乎我没有进入最后两个
.then(()
块,因为没有从它们记录控制台消息。
How can I properly handle the rejection error to avoid this error?如何正确处理拒绝错误以避免此错误? I've tried inserting and removing
status
in the resolve()
and reject()
statements but it doesn't seem to fix it.我试过在
resolve()
和reject()
语句中插入和删除status
,但它似乎没有修复它。
// 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();
}
});
});
});
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:就像评论已经说过的那样,
.catch(...)
将阻止您的异常冒泡成为unhandled rejection error
。
Alternatively you can also insert reject handler function for each of the .then(...)
clause.或者,您还可以为每个
.then(...)
子句插入拒绝处理函数。 That is, for each .then()
it should take in 2 functions, one for the happy path and the other for the bad path.也就是说,对于每个
.then()
它应该有 2 个函数,一个用于快乐路径,另一个用于错误路径。
Eg例如
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!");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.