From 4e55d19b78e8611356ea1af1e0184dc5a71e7f7b Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 29 Jun 2018 11:37:19 +0100 Subject: [PATCH] feat: logs path & querystring for requests (#796) Also maps 'Type' property from error responses --- src/utils/send-request.js | 5 +++++ test/files.spec.js | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/utils/send-request.js b/src/utils/send-request.js index eb9ea6adc..81a10cc06 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -9,6 +9,7 @@ const once = require('once') const streamToValue = require('./stream-to-value') const streamToJsonValue = require('./stream-to-json-value') const request = require('./request') +const log = require('debug')('ipfs-api:request') // -- Internal @@ -23,6 +24,7 @@ function parseError (res, cb) { if (payload) { error.code = payload.Code error.message = payload.Message || payload.toString() + error.type = payload.Type } cb(error) }) @@ -35,6 +37,8 @@ function onRes (buffer, cb) { const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0 + log(res.req.method, `${res.req.getHeaders().host}${res.req.path}`, res.statusCode, res.statusMessage) + if (res.statusCode >= 400 || !res.statusCode) { return parseError(res, cb) } @@ -162,6 +166,7 @@ function requestAPI (config, options, callback) { headers: headers, protocol: `${config.protocol}:` } + const req = request(config.protocol)(reqOptions, onRes(options.buffer, callback)) req.on('error', (err) => { diff --git a/test/files.spec.js b/test/files.spec.js index 160c73b56..05477c41b 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -457,10 +457,10 @@ describe('.files (the MFS API part)', function () { it('files.stat file that does not exist()', (done) => { ipfs.files.stat('/test-folder/does-not-exist()', (err, res) => { expect(err).to.exist() - if (err.code === 0) { - return done() - } - throw err + expect(err.code).to.equal(0) + expect(err.type).to.equal('error') + + done() }) })