Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: logs path & querystring for requests (#796)
Browse files Browse the repository at this point in the history
Also maps 'Type' property from error responses
  • Loading branch information
achingbrain authored and alanshaw committed Jun 29, 2018
1 parent 1a89701 commit 4e55d19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/utils/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
})
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

Expand Down

0 comments on commit 4e55d19

Please sign in to comment.