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

Commit

Permalink
fix: handle request errors in addFromURL
Browse files Browse the repository at this point in the history
  • Loading branch information
Donatas Stundys authored and daviddias committed Apr 23, 2018
1 parent e9876cf commit 7c5cea5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/util/url-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ module.exports = (send) => {
const validUrl = (url) => typeof url === 'string' && url.startsWith('http')

const requestWithRedirect = (url, opts, sendOneFile, callback) => {
request(parseUrl(url).protocol)(url, (res) => {
res.once('error', callback)
const req = request(parseUrl(url).protocol)(url, (res) => {
if (res.statusCode >= 400) {
return callback(new Error(`Failed to download with ${res.statusCode}`))
}
Expand All @@ -55,5 +54,9 @@ const requestWithRedirect = (url, opts, sendOneFile, callback) => {
}
sendOneFile(res, requestOpts, callback)
}
}).end()
})

req.once('error', callback)

req.end()
}
8 changes: 8 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ describe('.util', () => {
return ipfs.util.addFromURL('http://www.randomtext.me/#/gibberish', { onlyHash: true })
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
})

it('with invalid url', function (done) {
ipfs.util.addFromURL('http://invalid', (err, result) => {
expect(err.code).to.equal('ENOTFOUND')
expect(result).to.not.exist()
done()
})
})
})

describe('.getEndpointConfig', () => {
Expand Down

0 comments on commit 7c5cea5

Please sign in to comment.