我试图从指向不存在资源的URL加载TensorFlow fronzen模型来测试我的代码健壮性。但是,即使我设置了catch
,也无法管理功能ECONNREFUSED
在内部引发的tf.loadFrozenModel
。
是否可以缓解此问题?对我来说这是一个关键问题,因为它停止了nodejs的执行。
这是生成错误的代码。
global.fetch = require("node-fetch");
const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs-node");
class TFModel {
...
loadFzModel(modelUrl, modelWeigths) {
return tf.loadFrozenModel(modelUrl, modelWeigths)
.then((mod) => {
this.arch = mod;
})
.catch((err) => {
console.log("Error downloading the model!");
});
}
...
}
这里是我得到的错误:
UnhandledPromiseRejectionWarning: Error: http://localhost:30000/webModel/tensorflowjs_model.pb not found. FetchError: request to http://localhost:30000/webModel/tensorflowjs_model.pb failed, reason: connect ECONNREFUSED 127.0.0.1:30000
at BrowserHTTPRequest.<anonymous> (.../node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:128:31)
at step (.../node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:32:23)
at Object.throw (.../node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:13:53)
at rejected (.../node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:5:65)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
(node:23291) 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(). (rejection id: 1)
(node:23291) [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.
注意:如果modelUrl
和modelWeights
是指向现有资源的有效网址,则此代码有效。
节点2 :该代码作为Node-Red的自定义块的一部分执行。
答案 0 :(得分:0)
如果找不到其他解决方案,您可以像这样在顶层捕获错误:
process.on('uncaughtException', function (err) {
console.error(err);
});
在那里您可以获取更多特定信息,仅捕获特定错误。
答案 1 :(得分:0)