Dropbox 的 `filesGetTemporaryLink` 抛出无法捕获的 `UnhandledPromiseRejectionWarning` 并终止 Node.js
Dropbox's `filesGetTemporaryLink` throws an uncatchable `UnhandledPromiseRejectionWarning` and terminates Node.js
要下载 link 托管在 Dropbox 上的文件,我使用的是 Dropbox JavaScript API (7.0.0
):
export const fileDownload = async function fileDownload(fileUUID) {
let isSucceeded;
let message;
let file;
const dbx = _dropboxFactory();
try {
const operationResult = await dbx.filesGetTemporaryLink({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
if ("OK" === http.STATUS_CODES[operationResult.status].toUpperCase()) {
file = Object.freeze({
length: operationResult?.result?.metadata?.size,
link: operationResult?.result?.link,
mime: mime.lookup(operationResult?.result?.metadata?.name),
name: operationResult?.result?.metadata?.name
});
isSucceeded = true;
message = SYS_MESSAGES.storageFileDownloadSucceeded.code;
} else {
isSucceeded = false;
message = SYS_MESSAGES.storageFileDownloadFailed.code;
}
} catch (err) {
file = "error";
isSucceeded = false;
message = "FIL_NOT_FOUND";
}
const downloadResult = Object.freeze({
file,
isSucceeded,
message
});
return downloadResult;
};
问题是当文件的 path
错误时,我得到一个 Node.js 异常:
(node:9156) UnhandledPromiseRejectionWarning: #<Object>
(node:9156) 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()
. To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:9156) [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.
我测试了几个选项,得出的结论是问题出在:
const operationResult = await dbx.filesGetTemporaryLink({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
我无法理解的是,为什么 Node.js 声称 «未处理的承诺拒绝» 或 «未使用 [= 处理的承诺16=]» 并在生成它的代码被 try-catch
?
包装时抛出 UnhandledPromiseRejectionWarning
异常
开始 Node.js 15.x.x,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。因此,如何避免UnhandledPromiseRejectionWarning
?
临时解决方法:
到 运行 Node.js 带有标志 --unhandled-rejections=warn
.
这将防止 Node.js 进程在 UnhandledPromiseRejectionWarning
.
时以非零退出代码终止
尝试将您的 fileResponse
函数更改为类似这样的内容。您将 async
/await
与 .then().catch()
样式语法混淆了。
只需将您的函数包装在 try-catch
中
async function getDocument() {
try {
const response = await fetch(`${http() + ip}/downloadDocument`, {
body: JSON.stringify({fileUUID: oModel.oData[0].documentFile}),
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
method: "POST",
mode: "cors"
});
const data = await response.json();
return data;
} catch(err) {
console.log(err);
}
}
问题出在 Dropbox 库中,Dropbox 团队已在 7.1.0
版本中解决。升级后问题中的代码正常工作。
要下载 link 托管在 Dropbox 上的文件,我使用的是 Dropbox JavaScript API (7.0.0
):
export const fileDownload = async function fileDownload(fileUUID) {
let isSucceeded;
let message;
let file;
const dbx = _dropboxFactory();
try {
const operationResult = await dbx.filesGetTemporaryLink({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
if ("OK" === http.STATUS_CODES[operationResult.status].toUpperCase()) {
file = Object.freeze({
length: operationResult?.result?.metadata?.size,
link: operationResult?.result?.link,
mime: mime.lookup(operationResult?.result?.metadata?.name),
name: operationResult?.result?.metadata?.name
});
isSucceeded = true;
message = SYS_MESSAGES.storageFileDownloadSucceeded.code;
} else {
isSucceeded = false;
message = SYS_MESSAGES.storageFileDownloadFailed.code;
}
} catch (err) {
file = "error";
isSucceeded = false;
message = "FIL_NOT_FOUND";
}
const downloadResult = Object.freeze({
file,
isSucceeded,
message
});
return downloadResult;
};
问题是当文件的 path
错误时,我得到一个 Node.js 异常:
(node:9156) UnhandledPromiseRejectionWarning: #<Object>
(node:9156) 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()
. To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)(node:9156) [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.
我测试了几个选项,得出的结论是问题出在:
const operationResult = await dbx.filesGetTemporaryLink({
path: `/${CONFIG_STORAGE.uploader.assetsPath}/${fileUUID}`
});
我无法理解的是,为什么 Node.js 声称 «未处理的承诺拒绝» 或 «未使用 [= 处理的承诺16=]» 并在生成它的代码被 try-catch
?
UnhandledPromiseRejectionWarning
异常
开始 Node.js 15.x.x,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。因此,如何避免UnhandledPromiseRejectionWarning
?
临时解决方法:
到 运行 Node.js 带有标志 --unhandled-rejections=warn
.
这将防止 Node.js 进程在 UnhandledPromiseRejectionWarning
.
尝试将您的 fileResponse
函数更改为类似这样的内容。您将 async
/await
与 .then().catch()
样式语法混淆了。
只需将您的函数包装在 try-catch
async function getDocument() {
try {
const response = await fetch(`${http() + ip}/downloadDocument`, {
body: JSON.stringify({fileUUID: oModel.oData[0].documentFile}),
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json"
},
method: "POST",
mode: "cors"
});
const data = await response.json();
return data;
} catch(err) {
console.log(err);
}
}
问题出在 Dropbox 库中,Dropbox 团队已在 7.1.0
版本中解决。升级后问题中的代码正常工作。