Skip to content

Commit f33072d

Browse files
Merge pull request #91 from kazuhitoyokoi/master-updateswaagernodetemplate
Add information for error handling into swagger node template
2 parents cc84b42 + 72b0568 commit f33072d

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

templates/swagger/node.js.mustache

100755100644
Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,65 @@ module.exports = function (RED) {
6666
var result;
6767
{{#methods}}
6868
if (!errorFlag && node.method === '{{&methodName}}') {
69-
var parameters = [], nodeParam, nodeParamType;
69+
var {{&methodName}}_parameters = [];
70+
var {{&methodName}}_nodeParam;
71+
var {{&methodName}}_nodeParamType;
7072
{{#parameters}}
7173
{{#isBodyParam}}
7274
if (typeof msg.payload === 'object') {
73-
parameters.{{&camelCaseName}} = msg.payload;
75+
{{&methodName}}_parameters.{{&camelCaseName}} = msg.payload;
7476
} else {
75-
node.error('Unsupported type: \'' + (typeof msg.payload) + '\', '
76-
+ 'msg.payload must be JSON object or buffer.', msg);
77+
node.error('Unsupported type: \'' + (typeof msg.payload) + '\', ' + 'msg.payload must be JSON object or buffer.', msg);
7778
errorFlag = true;
7879
}
7980
{{/isBodyParam}}
8081
{{#isNotBodyParam}}
81-
nodeParam = node.{{&methodName}}_{{&camelCaseName}};
82-
nodeParamType = node.{{&methodName}}_{{&camelCaseName}}Type;
83-
parameters.{{&camelCaseName}} = nodeParamType === 'str' ? nodeParam || '' : RED.util.getMessageProperty(msg, nodeParam);
82+
{{&methodName}}_nodeParam = node.{{&methodName}}_{{&camelCaseName}};
83+
{{&methodName}}_nodeParamType = node.{{&methodName}}_{{&camelCaseName}}Type;
84+
if ({{&methodName}}_nodeParamType === 'str') {
85+
{{&methodName}}_parameters.{{&camelCaseName}} = {{&methodName}}_nodeParam || '';
86+
} else {
87+
{{&methodName}}_parameters.{{&camelCaseName}} = RED.util.getMessageProperty(msg, {{&methodName}}_nodeParam);
88+
}
8489
{{/isNotBodyParam}}
8590
{{/parameters}}
86-
87-
result = client.{{&methodName}}(parameters);
91+
result = client.{{&methodName}}({{&methodName}}_parameters);
8892
}
8993
{{/methods}}
9094
if (!errorFlag && result === undefined) {
9195
node.error('Method is not specified.', msg);
9296
errorFlag = true;
9397
}
94-
98+
var setData = function (msg, data) {
99+
if (data) {
100+
if (data.response) {
101+
if (data.response.statusCode) {
102+
msg.statusCode = data.response.statusCode;
103+
}
104+
if (data.response.headers) {
105+
msg.headers = data.response.headers;
106+
}
107+
if (data.response.request && data.response.request.uri && data.response.request.uri.href) {
108+
msg.responseUrl = data.response.request.uri.href;
109+
}
110+
}
111+
if (data.body) {
112+
msg.payload = data.body;
113+
}
114+
}
115+
return msg;
116+
};
95117
if (!errorFlag) {
96118
node.status({ fill: 'blue', shape: 'dot', text: '{{&className}}.status.requesting' });
97-
result.then(function (response) {
98-
if (response.body !== null && response.body !== undefined) {
99-
msg.payload = response.body;
100-
}
101-
node.send(msg);
119+
result.then(function (data) {
120+
node.send(setData(msg, data));
102121
node.status({});
103122
}).catch(function (error) {
104-
node.error(error, msg);
123+
var message = null;
124+
if (error && error.body && error.body.message) {
125+
message = error.body.message;
126+
}
127+
node.error(message, setData(msg, error));
105128
node.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.error' });
106129
});
107130
}

0 commit comments

Comments
 (0)