首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在openwhisk中使用trycatch组合器时出现的问题

在openwhisk中使用trycatch组合器时出现的问题
EN

Stack Overflow用户
提问于 2017-03-07 08:04:34
回答 1查看 80关注 0票数 0

我正在尝试使用openwhisk和redirect中的to catch combinator来捕获操作,以防我遇到错误,但我无法重定向。以下是我正在尝试使用的示例代码。

代码语言:javascript
复制
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 action中添加trycatch Combinator。

EN

回答 1

Stack Overflow用户

发布于 2017-03-07 23:09:54

为了在OpenWhisk中使用这个trycatch操作,首先需要在OpenWhisk中有另外两个可用操作。一个动作被称为try动作(在键tryName中定义)来调用,另一个是catch动作(在键catchName中定义),用于处理错误/异常。例如,我们需要将action1作为try操作,将action2作为catch操作。

如果我们使用CLI调用try call操作: wsk action invoke -br try call -p '$tryName‘action1 -p '$catchName’action2 -p param1 -p param2,正如您可能有更多参数,action1将首先被调用作为尝试。如果没有错误,则返回结果。这对action2来说没有什么麻烦。但是,如果调用action1时出现错误,将调用action2作为catch操作来处理错误,结果将是在处理错误时action2返回的结果。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42637859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档