Skip to content

Commit

Permalink
fix($server): Fix bug causing application to crash on empty response
Browse files Browse the repository at this point in the history
Implemented the recommended lodash changes to prevent a null pointer exception in the event that
assistant returns an empty response. If empty, the output text will be populated with content from
the generic object.
  • Loading branch information
noah-eigenfeld committed Aug 20, 2018
1 parent ac3a713 commit f2350e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
var AssistantV1 = require('watson-developer-cloud/assistant/v1'); // watson sdk
var _ = require('lodash'); // lodash

var app = express();

Expand Down Expand Up @@ -54,18 +55,28 @@ app.post('/api/message', function (req, res) {
return res.status(err.code || 500).json(err);
}

console.log("\n\nData.output:");

console.log(data.output);

// This is a fix for now, as since Assistant version 2018-07-10,
// output text can now be in output.generic.text
if (data.output.text.length === 0) {
if (data.output.generic !== undefined) {
if (data.output.generic[0].text !== undefined) {
data.output.text = data.output.generic[0].text;
} else if (data.output.generic[0].title !== undefined) {
var output = data.output;
if (output.text.length === 0 && _.has(output, 'generic')) {
var generic = output.generic;

if (_.isArray(generic)) {
if (_.has(generic[0], 'text')) {
data.output.text = generic[0].text;
} else if (_.has(generic[0], 'title')) {
data.output.text = data.output.generic[0].title;
}
}
}

console.log("Output text:");
console.log(data.output.text);

return res.json(updateMessage(payload, data));
});
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"body-parser": "^1.18.3",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"lodash": "^4.17.10",
"watson-developer-cloud": "^3.7.0"
},
"publishConfig": {
Expand Down

0 comments on commit f2350e0

Please sign in to comment.