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

Commit 7c5cea5

Browse files
Donatas Stundysdaviddias
Donatas Stundys
authored andcommitted
fix: handle request errors in addFromURL
1 parent e9876cf commit 7c5cea5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Diff for: src/util/url-add.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ module.exports = (send) => {
3535
const validUrl = (url) => typeof url === 'string' && url.startsWith('http')
3636

3737
const requestWithRedirect = (url, opts, sendOneFile, callback) => {
38-
request(parseUrl(url).protocol)(url, (res) => {
39-
res.once('error', callback)
38+
const req = request(parseUrl(url).protocol)(url, (res) => {
4039
if (res.statusCode >= 400) {
4140
return callback(new Error(`Failed to download with ${res.statusCode}`))
4241
}
@@ -55,5 +54,9 @@ const requestWithRedirect = (url, opts, sendOneFile, callback) => {
5554
}
5655
sendOneFile(res, requestOpts, callback)
5756
}
58-
}).end()
57+
})
58+
59+
req.once('error', callback)
60+
61+
req.end()
5962
}

Diff for: test/util.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ describe('.util', () => {
155155
return ipfs.util.addFromURL('http://www.randomtext.me/#/gibberish', { onlyHash: true })
156156
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
157157
})
158+
159+
it('with invalid url', function (done) {
160+
ipfs.util.addFromURL('http://invalid', (err, result) => {
161+
expect(err.code).to.equal('ENOTFOUND')
162+
expect(result).to.not.exist()
163+
done()
164+
})
165+
})
158166
})
159167

160168
describe('.getEndpointConfig', () => {

0 commit comments

Comments
 (0)