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

JSON.parse error reporting: with invalid json: in the error string #1001

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/utils/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ const log = require('debug')('ipfs-http-client:request')

// -- Internal

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

if (!isJson) {
return streamToValue(res, (err, data) => {
// the `err` here refers to errors in stream processing, which
// we ignore here, since we already have a valid `error` response
// from the server above that we have to report to the caller.
if (data) {
error.message = data.toString()
}
cb(error)
})
}

streamToJsonValue(res, (err, payload) => {
if (err) {
return cb(err)
Expand Down Expand Up @@ -44,7 +56,7 @@ function onRes (buffer, cb) {
}

if (res.statusCode >= 400 || !res.statusCode) {
return parseError(res, cb)
return parseError(res, cb, isJson)
}

// Return the response stream directly
Expand Down
2 changes: 1 addition & 1 deletion src/utils/stream-to-json-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function streamToJsonValue (res, cb) {
try {
res = JSON.parse(data)
} catch (err) {
return cb(err)
return cb(new Error(`Invalid JSON: ${data}`))
}

cb(null, res)
Expand Down