2017-03-07 62 views
0

我想在openwhisk中使用trycatch combinator,並在重定向時捕獲行爲以防萬一出現錯誤,但我無法重定向。以下是我嘗試使用的示例代碼。在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。

回答

0

爲了在OpenWhisk中使用此trycatch操作,首先需要在OpenWhisk中有兩個已經可用的其他操作。一個動作被稱爲try動作(在關鍵tryName中定義)來調用,另一個動作是catch動作(在關鍵字catchName中定義)來處理錯誤/ excpetion。例如,我們需要將action1作爲try動作,將action2作爲catch動作。

如果我們用CLI調用trycatch動作:wsk action invoke -br trycatch -p'$ tryName'action1 -p'$ catchName'action2 -p param1 -p param2,因爲你可能有更多的參數,action1是首先稱爲嘗試。如果沒有錯誤,結果將被返回。行動2沒有困擾。但是,如果調用action1時發生錯誤,則會將action2作爲處理錯誤的catch動作來調用,並且結果將是action2在處理錯誤時返回的結果。

如果你想看看如何在OpenWhisk中使用trycatch動作,這個動作的測試用例可以作爲一個很好的參考: 它從https://github.com/openwhisk/openwhisk-catalog/blob/master/tests/src/packages/combinators/CombinatorTests.scala#L144開始到這個文件的末尾。