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

Commit 4c2405f

Browse files
KrishnaPGAlan Shaw
authored and
Alan Shaw
committed
parseError taking non-JSON into account
Making the `parseError` function compatible with non-JSON responses from server. - `isJson = true` parameter added that is backwards compatible with existing code Ref: #1000 (comment)
1 parent d7e4fd6 commit 4c2405f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/utils/send-request.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ const log = require('debug')('ipfs-http-client:request')
1313

1414
// -- Internal
1515

16-
function parseError (res, cb) {
16+
function parseError (res, cb, isJson = true) {
1717
const error = new Error(`Server responded with ${res.statusCode}`)
1818

19+
if (!isJson) {
20+
return streamToValue(res, (err, data) => {
21+
// the `err` here refers to errors in stream processing, which
22+
// we ignore here, since we already have a valid `error` response
23+
// from the server above that we have to report to the caller.
24+
if (data) {
25+
error.message = data.toString()
26+
}
27+
cb(error)
28+
})
29+
}
30+
1931
streamToJsonValue(res, (err, payload) => {
2032
if (err) {
2133
return cb(err)
@@ -44,7 +56,7 @@ function onRes (buffer, cb) {
4456
}
4557

4658
if (res.statusCode >= 400 || !res.statusCode) {
47-
return parseError(res, cb)
59+
return parseError(res, cb, isJson)
4860
}
4961

5062
// Return the response stream directly

0 commit comments

Comments
 (0)