Skip to content

Commit 75256a6

Browse files
authored
fix: use parse-json (#26)
shows better errors when invalid
1 parent 8524620 commit 75256a6

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"debug": "^3.1.0",
1010
"is-retry-allowed": "^1.1.0",
1111
"is-stream": "^1.1.0",
12+
"parse-json": "^4.0.0",
1213
"tunnel-agent": "^0.6.0"
1314
},
1415
"devDependencies": {
@@ -18,6 +19,7 @@
1819
"@types/jest": "^23.3.1",
1920
"@types/nock": "^9.3.0",
2021
"@types/node": "^10.9.3",
22+
"@types/parse-json": "^4.0.0",
2123
"jest": "^23.5.0",
2224
"nock": "^9.6.1",
2325
"ts-jest": "^23.1.4",

src/http.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http = require('http')
2+
import JSONParse = require('parse-json')
23
import * as uri from 'url'
34
import * as util from 'util'
45

@@ -389,7 +390,7 @@ export class HTTP<T> {
389390
this.body = await concat(this.response) as T
390391
let type = this.response.headers['content-type'] ? deps.contentType.parse(this.response).type : ''
391392
let json = type.startsWith('application/json') || type.startsWith('application/vnd.api+json')
392-
if (json) this.body = JSON.parse(this.body as any as string)
393+
if (json) this.body = JSONParse(this.body as any as string)
393394
}
394395

395396
private _parseBody(body: object) {

yarn.lock

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
version "10.9.3"
5252
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.9.3.tgz#85f288502503ade0b3bfc049fe1777b05d0327d5"
5353

54+
"@types/parse-json@^4.0.0":
55+
version "4.0.0"
56+
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
57+
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
58+
5459
abab@^1.0.4:
5560
version "1.0.4"
5661
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.4.tgz#5faad9c2c07f60dd76770f71cf025b62a63cfd4e"
@@ -777,7 +782,7 @@ ecc-jsbn@~0.1.1:
777782
jsbn "~0.1.0"
778783
safer-buffer "^2.1.0"
779784

780-
error-ex@^1.2.0:
785+
error-ex@^1.2.0, error-ex@^1.3.1:
781786
version "1.3.2"
782787
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
783788
dependencies:
@@ -1875,6 +1880,11 @@ jsesc@^1.3.0:
18751880
version "1.3.0"
18761881
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b"
18771882

1883+
json-parse-better-errors@^1.0.1:
1884+
version "1.0.2"
1885+
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
1886+
integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==
1887+
18781888
json-schema-traverse@^0.3.0:
18791889
version "0.3.1"
18801890
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
@@ -2389,6 +2399,14 @@ parse-json@^2.2.0:
23892399
dependencies:
23902400
error-ex "^1.2.0"
23912401

2402+
parse-json@^4.0.0:
2403+
version "4.0.0"
2404+
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"
2405+
integrity sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=
2406+
dependencies:
2407+
error-ex "^1.3.1"
2408+
json-parse-better-errors "^1.0.1"
2409+
23922410
23932411
version "4.0.0"
23942412
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"

0 commit comments

Comments
 (0)