0

I am creating an assistant with IBM Watson. The assistant is passing some info to a web action that calls an https API and fetches data back to the assistant in a JSON format.

It looks like the web action is only working and now and then a message "error: The action did not produce a valid response and exited unexpectedly." pops up. It is not clear when or what triggers the fault. Invoking the exactly same action several times within seconds leads to the error message appearing randomly (I did not change anything in the action or code between calls). When there is no error, the code is working perfecly and providing the expected answer.

Here the compact code:

function main(msg){
  const https = require('https');
  var reqUrl = https URL;

return new Promise(function(resolve, reject) {
    https.get(reqUrl, (responseFromAPI) => {
        responseFromAPI.on('data', (chunk) => {
               completeResponse += chunk;
               let movie_info = JSON.parse(completeResponse);
               movie_info = movie_info.results[0];
               console.log(movie_info);
               resolve({movie_info});
        })
        responseFromAPI.on('error', (error) => {
            console.log(error);
            reject(error);
        });
    });
});
}

The error log follows:

[ "2019-06-06T14:35:32.697875Z stderr: undefined:1", "2019-06-06T14:35:32.697909Z stderr: {\"page\":1,\"total_results\":76,\"total_pages\":4,\"results\":[{\"vote_count\":39,\"id\":541560,\"video\":false,\"vote_average\":5,\"title\":\"The Wind\",\"popularity\":37.299,\"poster_path\":\"\/kcfPHZHSQODLCWdkUVLYATNyEVo.jpg\",\"original_language\":\"en\",\"original_title\":\"The Wind\",\"genre_ids\":[27,37,53],\"backdrop_path\":\"\/bqi6QBbXmkBar98HJJKEV1HFx71.jpg\",\"adult\":false,\"overview\":\"A supernatural thriller set in the Western frontier of the late 1800s, The Wind stars Caitlin Gerard as a plains-woman driven mad by the harshness and isolation of the untamed land.\",\"release_date\":\"2019-06-06\"},{\"vote_count\":2832,\"id\":353491,\"video\":false,\"vote_average\":5.6,\"title\":\"The Dark Tower\",\"popularity\":18.474,\"poster_path\":\"\/i9GUSgddIqrroubiLsvvMRYyRy0.jpg\",\"original_language\":\"en\",\"original_title\":\"The Dark Tower\",\"genre_ids\":[28,14,878,37,27],\"backdrop_path\":\"\/pVVobDO8cezhVPvwD6EBUN0g3mt.jpg\",\"adult\":false,\"overview\":\"The last Gunslinger, Roland Deschain, has been locked in an eternal battle with Walter O’Dim, also known as the", "2019-06-06T14:35:32.697914Z stderr: ", "2019-06-06T14:35:32.697951Z stderr: SyntaxError: Unexpected end of JSON input", "2019-06-06T14:35:32.697955Z stderr: at JSON.parse ()", "2019-06-06T14:35:32.697959Z stderr: at IncomingMessage.responseFromAPI.on (eval at NodeActionRunner.init (/nodejsAction/runner.js:79:45), :10:38)", "2019-06-06T14:35:32.697963Z stderr: at IncomingMessage.emit (events.js:189:13)", "2019-06-06T14:35:32.697968Z stderr: at IncomingMessage.Readable.read (_stream_readable.js:487:10)", "2019-06-06T14:35:32.697972Z stderr: at flow (_stream_readable.js:931:34)", "2019-06-06T14:35:32.697976Z stderr: at resume_ (_stream_readable.js:912:3)", "2019-06-06T14:35:32.697980Z stderr: at process._tickCallback (internal/process/next_tick.js:63:19)", "unknown unknown: There was an issue while collecting your logs. Data might be missing." ]

movie_info is a list of movies and data, hence the code only picks up the data in the position [0] (here the movie "The wind").

I contacted the API hosts and they said the API is working perfectly, hence the problem may come from the web action itself.

Any help is appreciated.

Triferus
  • 15
  • 5
  • That was quick and great @Yaroslav Gaponov, thanks a lot!!! My promise structure was used without problems for an http call. Why does it need to be changed here? I am a newbie, so any documentation is appreciated :) – Triferus Jun 06 '19 at 15:07

1 Answers1

1

You parse not all response but only first chunk

function main(msg){
  const https = require('https');
  var reqUrl = https URL;

return new Promise(function(resolve, reject) {
    https.get(reqUrl, (responseFromAPI) => {
       const chunks = [];
        responseFromAPI
          .on('data', chunk => chunks.push(chunk))
          .on('end', _=> {
               let movie_info = JSON.parse(Buffer.concat(chunks));
               movie_info = movie_info.results[0];
               console.log(movie_info);
               resolve({movie_info});
          })
          .on('error', (error) => {
            console.log(error);
            reject(error);
        });
    });
});
}
Yaroslav Gaponov
  • 1,997
  • 13
  • 12