You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2018/07/18 13:58:32 UTC

[couchdb-nano] 02/08: refactor responseHandler to make parameter names more readable

This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch issue98
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git

commit 6eac47d3882361e81ee973c322176cb8eb93f1c9
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Wed Jul 18 13:22:54 2018 +0100

    refactor responseHandler to make parameter names more readable
---
 lib/nano.js | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/lib/nano.js b/lib/nano.js
index fe72b73..e0384cb 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -69,14 +69,15 @@ module.exports = exports = nano = function dbScope(cfg) {
   }
   const responseHandler = function(req, opts, resolve, reject, callback) {
     
-    return function(e, h, b) {
+    return function(err, response, body) {
       let parsed;
-      const rh = h && h.headers || {};
-      rh.statusCode = h && h.statusCode || 500;
-      rh.uri = req.uri;
-      if (e) {
-        log({err: 'socket', body: b, headers: rh});
-        const ret_e = errs.merge(e, {
+      const responseHeaders = Object.assign({
+        uri: req.uri,
+        statusCode: response ? response.statusCode : 500
+      }, response ? response.headers : {});
+      if (err) {
+        log({err: 'socket', body: body, headers: responseHeaders});
+        const ret_e = errs.merge(err, {
           message: 'error happened in your connection',
           scope: 'socket',
           errid: 'request'
@@ -90,27 +91,27 @@ module.exports = exports = nano = function dbScope(cfg) {
         return ;
       }
 
-      delete rh.server;
-      delete rh['content-length'];
+      delete responseHeaders.server;
+      delete responseHeaders['content-length'];
 
       if (opts.dontParse) {
-        parsed = b;
+        parsed = body;
       } else {
-        try { parsed = JSON.parse(b); } catch (err) { parsed = b; }
+        try { parsed = JSON.parse(body); } catch (err) { parsed = body; }
       }
 
-      if (rh.statusCode >= 200 && rh.statusCode < 400) {
-        log({err: null, body: parsed, headers: rh});
+      if (responseHeaders.statusCode >= 200 && responseHeaders.statusCode < 400) {
+        log({err: null, body: parsed, headers: responseHeaders});
         if (resolve) {
           resolve(parsed);
         } 
         if (callback) {
-          callback(null, parsed, rh);
+          callback(null, parsed, responseHeaders);
         }
         return;
       }
 
-      log({err: 'couch', body: parsed, headers: rh});
+      log({err: 'couch', body: parsed, headers: responseHeaders});
 
       // cloudant stacktrace
       if (typeof parsed === 'string') {
@@ -126,17 +127,17 @@ module.exports = exports = nano = function dbScope(cfg) {
 
       // scrub credentials
       req.uri = scrub(req.uri);
-      rh.uri = scrub(rh.uri);
+      responseHeaders.uri = scrub(responseHeaders.uri);
       if (req.headers.cookie) {
         req.headers.cookie = "XXXXXXX";
       }
 
       let errors = errs.merge({
-        message: 'couch returned ' + rh.statusCode,
+        message: 'couch returned ' + responseHeaders.statusCode,
         scope: 'couch',
-        statusCode: rh.statusCode,
+        statusCode: responseHeaders.statusCode,
         request: req,
-        headers: rh,
+        headers: responseHeaders,
         errid: 'non_200'
       }, errs.create(parsed));