我正在尝试在 openwhisk 中使用 trycatch 组合器并重定向以捕获操作,以防出现错误,但我无法重定向。以下是我正在尝试的示例代码。
var openwhisk = require('openwhisk')
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
var visualrecognition = new VisualRecognitionV3({
api_key: '******',
version_date: '***'});
var pkgcloud = require('pkgcloud');
var osConfig = {
provider: 'openstack',
useServiceCatalog: true,
useInternal: false,
keystoneAuthVersion: 'v3',
authUrl: '*****',
tenantId: '******',
domainId: '********',
username: 'admin_******',
password: '******',
region: 'dallas'};
var client = pkgcloud.storage.createClient(osConfig);
function main(params) {
return new Promise(function(resolve, reject) {
var options = {
container: params.containername || 'my-container',
remote: params.filename || 'openwhisk.jpg',
local: 'test.jpg'
};
client.download(options, function(err, result) {
var params = {
images_file: fs.createReadStream('test.jpg')
};
visualrecognition.classify(params, function(err, res) {
if (err) {
const wsk = openwhisk({ignore_certs: params['$ignore_certs'] || false})
const catchName = "hello"
return wsk.actions
.invoke({
actionName: catchName,
params: catchArgs,
blocking: true
})
.then(activation => activation.response.result)
.catch(error => {
try {
// if the action ran and failed, the result field is guaranteed
// to contain an error field causing the overall action to fail
// with that error
return error.error.response.result
} catch (e) {
return {
error: {
message: `There was a problem invoking ${catchName}.`,
cause: error.error
}
}
}
})
} else {
var json = {
container: params.containername,
filename: params.filename
};
var length = res.images[0].classifiers[0].classes.length;
json.score = 0;
var type_hierarchy = "";
for (i = 0; i < length; i++) {
var score = res.images[0].classifiers[0].classes[i].score;
var classes = res.images[0].classifiers[0].classes[i].class;
var htype = res.images[0].classifiers[0].classes[i].type_hierarchy;
if (score > json.score) {
json.score = score;
json.class = classes;
json.type_hierarchy = htype || "test";
}
}
console.log(json);
resolve(json);
}
});
});
});};
如何在 Openwhisk nodejs 操作中添加 trycatch 组合器。