Skip to content

Commit f2350e0

Browse files
fix($server): Fix bug causing application to crash on empty response
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.
1 parent ac3a713 commit f2350e0

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

app.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var express = require('express'); // app server
2020
var bodyParser = require('body-parser'); // parser for post requests
2121
var AssistantV1 = require('watson-developer-cloud/assistant/v1'); // watson sdk
22+
var _ = require('lodash'); // lodash
2223

2324
var app = express();
2425

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

58+
console.log("\n\nData.output:");
59+
60+
console.log(data.output);
61+
5762
// This is a fix for now, as since Assistant version 2018-07-10,
5863
// output text can now be in output.generic.text
59-
if (data.output.text.length === 0) {
60-
if (data.output.generic !== undefined) {
61-
if (data.output.generic[0].text !== undefined) {
62-
data.output.text = data.output.generic[0].text;
63-
} else if (data.output.generic[0].title !== undefined) {
64+
var output = data.output;
65+
if (output.text.length === 0 && _.has(output, 'generic')) {
66+
var generic = output.generic;
67+
68+
if (_.isArray(generic)) {
69+
if (_.has(generic[0], 'text')) {
70+
data.output.text = generic[0].text;
71+
} else if (_.has(generic[0], 'title')) {
6472
data.output.text = data.output.generic[0].title;
6573
}
6674
}
6775
}
6876

77+
console.log("Output text:");
78+
console.log(data.output.text);
79+
6980
return res.json(updateMessage(payload, data));
7081
});
7182
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"body-parser": "^1.18.3",
2323
"dotenv": "^6.0.0",
2424
"express": "^4.16.3",
25+
"lodash": "^4.17.10",
2526
"watson-developer-cloud": "^3.7.0"
2627
},
2728
"publishConfig": {

0 commit comments

Comments
 (0)