【发布时间】:2023-03-25 13:59:01
【问题描述】:
概述
我有一个 node.js 应用程序,它通过 HTTP 调用外部 API,并对响应标头中返回的 content-disposition 标头值进行正则表达式验证和捕获。我目前将其编码为 try/catch 块;但是,我已经阅读了大量资料,这些资料似乎一般都避开了这种方法。
我的问题
我的问题是,我无法从上述来源中完全辨别 my 特定的 try/catch 实例是否不安全或可能容易出错,因为在某些情况下,node.js 中存在 try/catch 的情况。 js是合适的(例如json解析)。下面显示的 try/catch 块是否有更好的替代方法?
我的 Try/Catch 块
var parseFileName = function (contentDisposition) {
"use strict";
return /filename[^;=\n]*=[\\'"]*((['"]).*?\2|[^;'"\n]*)/g.exec(contentDisposition)[1];
};
try {
contentDisposition = response.headers["content-disposition"];
fileName = parseFileName(contentDisposition);
} catch (e) {
console.error(e);
return next(new Error("Content Disposition parse failed"));
}
关闭
非常感谢您提供任何见解或帮助。
最好的,
克里斯
【问题讨论】:
标签: node.js error-handling try-catch