Skip to content

Commit

Permalink
added debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Dickey committed Jul 31, 2016
1 parent 5f62be3 commit 3c139ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
43 changes: 19 additions & 24 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

const debug = require('debug')('http')
const debugHeaders = require('debug')('http:headers')

/*
* Object capable of making API calls.
*/
Expand Down Expand Up @@ -122,9 +125,9 @@ class Request {
return
}
concat(res).then((data) => {
if (this.debug) {
console.error('<-- ' + data)
}
debug(`<-- ${this.options.method} ${this.options.path}\n${data}`)
debugHeaders('\n' + renderHeaders(res.headers))
if (this.debug) console.error('<-- ' + data)
if (res.statusCode.toString().match(/^2\d{2}$/)) {
this.handleSuccess(res, data)
} else {
Expand All @@ -150,12 +153,11 @@ class Request {
}

logRequest (req) {
if (this.debug) {
console.error('--> ' + req.method + ' ' + req.path)
}
if (this.debugHeaders) {
printHeaders(req._headers)
}
let headers = renderHeaders(req._headers)
debug(`--> ${req.method} ${this.options.path}`)
debugHeaders('\n' + headers)
if (this.debug) console.error('--> ' + req.method + ' ' + req.path)
if (this.debugHeaders) console.error(headers)
}

/*
Expand All @@ -169,12 +171,9 @@ class Request {
request_id: res.headers['request-id']
})
}
if (this.debug) {
console.error('<-- ' + res.statusCode + ' ' + res.statusMessage)
}
if (this.debugHeaders) {
printHeaders(res.headers)
}
let headers = renderHeaders(res.headers)
if (this.debug) console.error('<-- ' + res.statusCode + ' ' + res.statusMessage)
if (this.debugHeaders) console.error(headers)
}

/*
Expand Down Expand Up @@ -321,15 +320,11 @@ function getCerts (debug) {
})
}

function printHeaders (headers) {
var key
var value
for (key in headers) {
if (headers.hasOwnProperty(key)) {
value = key.toUpperCase() === 'AUTHORIZATION' ? 'REDACTED' : headers[key]
console.error(' ' + key + '=' + value)
}
}
function renderHeaders (headers) {
return Object.keys(headers).map(key => {
let value = key.toUpperCase() === 'AUTHORIZATION' ? 'REDACTED' : headers[key]
return ' ' + key + '=' + value
}).join('\n')
}

function concat (stream) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"Sehrope Sarkuni"
],
"dependencies": {
"debug": "2.2.0",
"is-retry-allowed": "^1.0.0",
"tunnel-agent": "^0.4.0"
},
Expand Down

0 comments on commit 3c139ad

Please sign in to comment.