Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from ypjin/master

NODEJS-995: acs-node keeps throwing exception upon requests to ACS timeout
  • Loading branch information...
commit 88e2a7596b8f58d893ffc676c39acfd1e98a767e 2 parents ef266ae + 076c738
Wei Kong baobeimm authored
Showing with 27 additions and 3 deletions.
  1. +27 −3 lib/util/utils.js
30 lib/util/utils.js
View
@@ -108,9 +108,19 @@ exports.sendRequest = function(host, port, path, method, data, header, useSecure
var req = protocal.request(options);
req.on('response', function(response) {
- function performOnload(body) {
+
+ var statusCode = response.statusCode;
+
+ function performOnload(body) {
if(body && body.trim().length > 0) {
- var data = JSON.parse(body);
+
+ try {
+ var data = JSON.parse(body);
+ } catch(E) {
+ if(statusCode == 200) statusCode = 500;
+ return callback(buildErrResponse(statusCode, body));
+ }
+
if(data && data.meta) {
if(data.meta.session_id) {
var sessionId = data.meta.session_id;
@@ -170,9 +180,19 @@ exports.sendRequestWithFile = function(host, port, path, method, data, header, u
var req = protocal.request(options);
req.on('response', function(response) {
+
+ var statusCode = response.statusCode;
+
function performOnload(body) {
if(body && body.trim().length > 0) {
- var data = JSON.parse(body);
+
+ try {
+ var data = JSON.parse(body);
+ } catch(E) {
+ if(statusCode == 200) statusCode = 500;
+ return callback(buildErrResponse(statusCode, body));
+ }
+
if(data && data.meta) {
if(data.meta.session_id) {
var sessionId = data.meta.session_id;
@@ -236,3 +256,7 @@ function prepareRequestBody(params, filePropName, fileName, fileBinary, mimeType
callback(post_data, boundary);
}
+
+function buildErrResponse(code, message) {
+ return {'meta' : {'status': 'fail', 'code': code, 'message': message}};
+}
Please sign in to comment.
Something went wrong with that request. Please try again.