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

Commit

Permalink
fix(json): improve JSON parse error reporting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zkat committed May 24, 2017
1 parent a81145c commit 1c810df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
},
"dependencies": {
"encoding": "^0.1.11",
"json-parse-helpfulerror": "^1.0.3",
"safe-buffer": "^5.0.1"
}
}
3 changes: 2 additions & 1 deletion src/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Buffer = require('safe-buffer').Buffer
const Blob = require('./blob.js')
const BUFFER = Blob.BUFFER
const convert = require('encoding').convert
const parseJson = require('json-parse-helpfulerror').parse
const FetchError = require('./fetch-error.js')
const Stream = require('stream')

Expand Down Expand Up @@ -92,7 +93,7 @@ Body.prototype = {
* @return Promise
*/
json () {
return consumeBody.call(this).then(buffer => JSON.parse(buffer.toString()))
return consumeBody.call(this).then(buffer => parseJson(buffer.toString()))
},

/**
Expand Down

0 comments on commit 1c810df

Please sign in to comment.