Skip to content

Commit b39ecda

Browse files
kazuhitoyokoiHiroyasuNishiyama
authored andcommitted
Add error handling for configuration node (#90)
1 parent 3ba4c29 commit b39ecda

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

templates/swagger/node.js.mustache

100644100755
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"use strict";
1+
'use strict';
22
var lib = require('./lib.js');
33

44
module.exports = function (RED) {
@@ -8,27 +8,31 @@ module.exports = function (RED) {
88
this.service = RED.nodes.getNode(config.service);
99
{{/hasServiceParams}}
1010
this.method = config.method;
11-
1211
{{#methods}}
1312
{{#parameters}}
1413
this.{{&methodName}}_{{&camelCaseName}} = config.{{&methodName}}_{{&camelCaseName}};
1514
this.{{&methodName}}_{{&camelCaseName}}Type = config.{{&methodName}}_{{&camelCaseName}}Type || 'str';
1615
{{/parameters}}
1716
{{/methods}}
18-
1917
var node = this;
2018

2119
node.on('input', function (msg) {
20+
var errorFlag = false;
2221
{{#domain}}
2322
var client = new lib.{{&className}}();
2423
{{/domain}}
2524
{{^domain}}
26-
var client = new lib.{{&className}}({ domain: this.service.host });
25+
var client;
26+
if (this.service && this.service.host) {
27+
client = new lib.{{&className}}({ domain: this.service.host });
28+
} else {
29+
node.error('Host in configuration node is not specified.', msg);
30+
errorFlag = true;
31+
}
2732
{{/domain}}
28-
2933
{{#isSecure}}
3034
{{#isSecureToken}}
31-
if (this.service && this.service.credentials && this.service.credentials.secureTokenValue) {
35+
if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureTokenValue) {
3236
if (this.service.secureTokenIsQuery) {
3337
client.setToken(this.service.credentials.secureTokenValue,
3438
this.service.secureTokenHeaderOrQueryName, true);
@@ -39,7 +43,7 @@ module.exports = function (RED) {
3943
}
4044
{{/isSecureToken}}
4145
{{#isSecureApiKey}}
42-
if (this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
46+
if (!errorFlag && this.service && this.service.credentials && this.service.credentials.secureApiKeyValue) {
4347
if (this.service.secureApiKeyIsQuery) {
4448
client.setApiKey(this.service.credentials.secureApiKeyValue,
4549
this.service.secureApiKeyHeaderOrQueryName, true);
@@ -50,18 +54,18 @@ module.exports = function (RED) {
5054
}
5155
{{/isSecureApiKey}}
5256
{{#isSecureBasic}}
53-
if (this.service && this.service.credentials) {
57+
if (!errorFlag && this.service && this.service.credentials) {
5458
client.setBasicAuth(this.service.credentials.username, this.service.credentials.password);
5559
}
5660
{{/isSecureBasic}}
5761
{{/isSecure}}
58-
59-
client.body = msg.payload;
62+
if (!errorFlag) {
63+
client.body = msg.payload;
64+
}
6065

6166
var result;
62-
var errorFlag = false;
6367
{{#methods}}
64-
if (node.method === '{{&methodName}}') {
68+
if (!errorFlag && node.method === '{{&methodName}}') {
6569
var parameters = [], nodeParam, nodeParamType;
6670
{{#parameters}}
6771
{{#isBodyParam}}
@@ -82,10 +86,14 @@ module.exports = function (RED) {
8286

8387
result = client.{{&methodName}}(parameters);
8488
}
85-
8689
{{/methods}}
90+
if (!errorFlag && result === undefined) {
91+
node.error('Method is not specified.', msg);
92+
errorFlag = true;
93+
}
94+
8795
if (!errorFlag) {
88-
node.status({ fill: "blue", shape: "dot", text: "{{&className}}.status.requesting" });
96+
node.status({ fill: 'blue', shape: 'dot', text: '{{&className}}.status.requesting' });
8997
result.then(function (response) {
9098
if (response.body !== null && response.body !== undefined) {
9199
msg.payload = response.body;
@@ -94,14 +102,13 @@ module.exports = function (RED) {
94102
node.status({});
95103
}).catch(function (error) {
96104
node.error(error, msg);
97-
node.status({ fill: "red", shape: "ring", text: "node-red:common.status.error" });
105+
node.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.error' });
98106
});
99107
}
100108
});
101109
}
102110

103-
RED.nodes.registerType("{{&nodeName}}", {{&className}}Node);
104-
111+
RED.nodes.registerType('{{&nodeName}}', {{&className}}Node);
105112
{{#hasServiceParams}}
106113
function {{&className}}ServiceNode(n) {
107114
RED.nodes.createNode(this, n);
@@ -127,21 +134,21 @@ module.exports = function (RED) {
127134
{{/isSecure}}
128135
}
129136

130-
RED.nodes.registerType("{{&nodeName}}-service", {{&className}}ServiceNode, {
137+
RED.nodes.registerType('{{&nodeName}}-service', {{&className}}ServiceNode, {
131138
credentials: {
132139
{{#isSecure}}
133140
{{#isSecureToken}}
134-
secureTokenValue: { type: "password" },
141+
secureTokenValue: { type: 'password' },
135142
{{/isSecureToken}}
136143
{{#isSecureApiKey}}
137-
secureApiKeyValue: { type: "password" },
144+
secureApiKeyValue: { type: 'password' },
138145
{{/isSecureApiKey}}
139146
{{#isSecureBasic}}
140-
username: { type: "text" },
141-
password: { type: "password" },
147+
username: { type: 'text' },
148+
password: { type: 'password' },
142149
{{/isSecureBasic}}
143150
{{/isSecure}}
144-
temp: { type: "text" }
151+
temp: { type: 'text' }
145152
}
146153
});
147154
{{/hasServiceParams}}

0 commit comments

Comments
 (0)