0

I am trying to use trycatch combinator in openwhisk and redirect to catch action in case I got error, but I am unable to redirect. Following is the sample code that I am trying with.

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);
            }
        });
    });
});};

How to add trycatch Combinator in Openwhisk nodejs action.

Mani Teja
  • 669
  • 1
  • 5
  • 9

1 Answers1

0

In order to use this trycatch action in OpenWhisk, first you need to have two other actions already available in OpenWhisk. One action is used called as the try action(defined in the key tryName) to call, the other is the catch action(defined in the key catchName) to process tha error/excpetion. For example, we need to have action1 as the try action and action2 as the catch action.

If we call the trycatch action with the CLI: wsk action invoke -br trycatch -p '$tryName' action1 -p '$catchName' action2 -p param1 -p param2 , as you may have more parameters, action1 is called first as a try. If there is no error, the result will be returned. There is no bother to the action2. However, if there is an error calling action1, action2 will be called as a catch action to handle the error, and the result will be the result action2 returns when the error is processed.

If you would like to see how to use trycatch action in OpenWhisk, the test case of this action can be a good reference: It starts at https://github.com/openwhisk/openwhisk-catalog/blob/master/tests/src/packages/combinators/CombinatorTests.scala#L144 to the end of this file.