Skip to content

Commit 3b3bdc5

Browse files
committed
Reject non-JSON responses in the form of an error instead of throwing an exception.
1 parent 0bb4bc5 commit 3b3bdc5

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,18 @@ function ConManager(baseurl, token_name) {
113113
resp += chunk;
114114
});
115115
res.on('end', () => {
116-
if (resp)
117-
resp = JSON.parse(resp);
116+
if (resp) {
117+
try {
118+
resp = JSON.parse(resp);
119+
} catch (e) {
120+
if (e instanceof SyntaxError) {
121+
resp = { 'httpErrorCode': res.statusCode, 'code': res.statusMessage, 'description': resp };
122+
res.statusCode = 500;
123+
} else {
124+
throw e;
125+
}
126+
}
127+
}
118128

119129
debug(resp);
120130
debug("==================================================");
@@ -136,8 +146,18 @@ function ConManager(baseurl, token_name) {
136146
nresp += chunk;
137147
});
138148
nres.on('end', () => {
139-
if (nresp)
140-
nresp = JSON.parse(nresp);
149+
if (nresp) {
150+
try {
151+
nresp = JSON.parse(nresp);
152+
} catch (e) {
153+
if (e instanceof SyntaxError) {
154+
nresp = { 'httpErrorCode': nres.statusCode, 'code': nres.statusMessage, 'description': nresp };
155+
nres.statusCode = 500;
156+
} else {
157+
throw e;
158+
}
159+
}
160+
}
141161

142162
debug(nresp);
143163
debug("==================================================");
@@ -504,6 +524,6 @@ function ConManager(baseurl, token_name) {
504524
*/
505525

506526
this.login_response = {};
507-
};
527+
}
508528

509529
exports.ConManager = ConManager;

0 commit comments

Comments
 (0)