Skip to content
This repository was archived by the owner on Aug 18, 2022. It is now read-only.

Commit 1c810df

Browse files
committed
fix(json): improve JSON parse error reporting
It's not uncommon for services to send back bad JSON on errors, specially things like proxies, or if you get the wrong URI. This way, we can actually find out what the bad JSON was.
1 parent a81145c commit 1c810df

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

package-lock.json

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
},
7070
"dependencies": {
7171
"encoding": "^0.1.11",
72+
"json-parse-helpfulerror": "^1.0.3",
7273
"safe-buffer": "^5.0.1"
7374
}
7475
}

src/body.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Buffer = require('safe-buffer').Buffer
1111
const Blob = require('./blob.js')
1212
const BUFFER = Blob.BUFFER
1313
const convert = require('encoding').convert
14+
const parseJson = require('json-parse-helpfulerror').parse
1415
const FetchError = require('./fetch-error.js')
1516
const Stream = require('stream')
1617

@@ -92,7 +93,7 @@ Body.prototype = {
9293
* @return Promise
9394
*/
9495
json () {
95-
return consumeBody.call(this).then(buffer => JSON.parse(buffer.toString()))
96+
return consumeBody.call(this).then(buffer => parseJson(buffer.toString()))
9697
},
9798

9899
/**

0 commit comments

Comments
 (0)