From dc0f19fe9eebe69c3999577a92a1186695486165 Mon Sep 17 00:00:00 2001 From: Powpow Shen Date: Wed, 29 Oct 2014 19:28:37 +0800 Subject: [PATCH 1/4] [fix] TypeError: Object.defineProperties called on non-object (issue #166) --- lib/cradle.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/cradle.js b/lib/cradle.js index b21d5ef..2876feb 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -213,23 +213,17 @@ cradle.Connection.prototype.request = function (options, callback) { ); } return callback(err); - } - else if (options.method === 'HEAD') { - return callback(null, res.headers, res.statusCode); - } - else if (body && body.error) { - cradle.extend(body, { headers: res.headers }); - body.headers.status = res.statusCode; - return callback(new cradle.CouchError(body)); - } - - try { body = JSON.parse(body) } - catch (err) { } - - if (body && body.error) { - cradle.extend(body, { headers: res.headers }); - body.headers.status = res.statusCode; - return callback(new cradle.CouchError(body)); + } else { + try { body = JSON.parse(body); } + catch (error) { } + if (options.method === 'HEAD') { + return callback(null, res.headers, res.statusCode); + } + else if (body && body.error) { + cradle.extend(body, { headers: res.headers }); + body.headers.status = res.statusCode; + return callback(new cradle.CouchError(body)); + } } callback(null, self.options.raw ? body : new cradle.Response(body, res)); From 8bafe8583ad3a471f5bbb2b0c79c46638e574eea Mon Sep 17 00:00:00 2001 From: Powpow Shen Date: Wed, 29 Oct 2014 19:41:27 +0800 Subject: [PATCH 2/4] [fix] TypeError: Object.defineProperties called on non-object (issue #166) --- lib/cradle.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/cradle.js b/lib/cradle.js index 2876feb..7933ea3 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -214,8 +214,10 @@ cradle.Connection.prototype.request = function (options, callback) { } return callback(err); } else { - try { body = JSON.parse(body); } - catch (error) { } + if (typeof body === 'string') { + try { body = JSON.parse(body); } + catch (error) { } + } if (options.method === 'HEAD') { return callback(null, res.headers, res.statusCode); } From cbba8ee3a03aa013b9b0281f22b51b3474902c18 Mon Sep 17 00:00:00 2001 From: Powpow Shen Date: Fri, 26 Dec 2014 00:54:12 +0800 Subject: [PATCH 3/4] Remove unecessary condition block. --- lib/cradle.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cradle.js b/lib/cradle.js index 7933ea3..757f90d 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -214,10 +214,10 @@ cradle.Connection.prototype.request = function (options, callback) { } return callback(err); } else { - if (typeof body === 'string') { - try { body = JSON.parse(body); } - catch (error) { } - } + + try { body = JSON.parse(body); } + catch (error) { } + if (options.method === 'HEAD') { return callback(null, res.headers, res.statusCode); } From 510336812ed2d8f2413d6bb4cd199f189d0dbabf Mon Sep 17 00:00:00 2001 From: Powpow Shen Date: Fri, 26 Dec 2014 12:59:47 +0800 Subject: [PATCH 4/4] Refactor. --- lib/cradle.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/cradle.js b/lib/cradle.js index 757f90d..5537c7e 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -213,20 +213,20 @@ cradle.Connection.prototype.request = function (options, callback) { ); } return callback(err); - } else { - + } + else if (options.method === 'HEAD') { + return callback(null, res.headers, res.statusCode); + } + else if (body) { try { body = JSON.parse(body); } catch (error) { } - - if (options.method === 'HEAD') { - return callback(null, res.headers, res.statusCode); - } - else if (body && body.error) { + if (body.error) { cradle.extend(body, { headers: res.headers }); body.headers.status = res.statusCode; return callback(new cradle.CouchError(body)); } } + callback(null, self.options.raw ? body : new cradle.Response(body, res)); });