在openwhisk

时间:2017-03-07 00:04:34

标签: try-catch combinators openwhisk

我正在尝试在openwhisk中使用trycatch combinator并重定向以捕获操作,以防我收到错误,但我无法重定向。以下是我正在尝试的示例代码。

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 Combinator。

1 个答案:

答案 0 :(得分:0)

为了在OpenWhisk中使用此trycatch操作,首先需要在OpenWhisk中提供另外两个其他操作。一个操作被称为try动作(在键tryName中定义)来调用,另一个是catch动作(在key catchName中定义)来处理错误/ excpetion。例如,我们需要将action1作为try动作,将action2作为catch动作。

如果我们使用CLI调用trycatch操作:wsk action invoke -br trycatch -p&#39; $ tryName&#39; action1 -p&#39; $ catchName&#39; action2 -p param1 -p param2,因为你可能有更多参数,所以首先调用action1作为try。如果没有错误,将返回结果。动作2没有任何麻烦。但是,如果调用action1时出错,则action2将被调用为catch操作来处理错误,结果将是处理错误时action2返回的结果。

如果您想了解如何在OpenWhisk中使用trycatch操作,此操作的测试用例可以是一个很好的参考: 它从https://github.com/openwhisk/openwhisk-catalog/blob/master/tests/src/packages/combinators/CombinatorTests.scala#L144开始到此文件的末尾。