Skip to content

Commit

Permalink
fix(json): don't catch body errors (#64)
Browse files Browse the repository at this point in the history
The thrown error suffices, we only catch parse errors.
  • Loading branch information
wraithgar committed Aug 22, 2022
1 parent 598fab9 commit 9658a0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class Body {
}

async json () {
const buf = await this[CONSUME_BODY]()
try {
const buf = await this[CONSUME_BODY]()
return JSON.parse(buf.toString())
} catch (er) {
throw new FetchError(
Expand Down
14 changes: 13 additions & 1 deletion test/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ t.test('more static method coverage', async t => {
t.equal(Body.getTotalBytes({ body: {} }), null)
})

t.test('json', async t => {
t.test('json FetchError', async t => {
t.same(await new Body('{"a":1}').json(), { a: 1 })
await t.rejects(Object.assign(new Body('a=1'), { url: 'asdf' }).json(), {
name: 'FetchError',
Expand All @@ -263,6 +263,18 @@ t.test('json', async t => {
})
})

t.test('json body error', async t => {
const s = new PassThrough()
const b = new Body(s)
b.url = 'xyz'
setTimeout(() => s.emit('error', new RangeError('hello')))
await t.rejects(b.json(), {
name: 'FetchError',
message: 'Could not create Buffer from response body for xyz: hello',
type: 'system',
})
})

t.test('handles environments where setTimeout does not have unref', async t => {
const originalSetTimeout = setTimeout
// simulate environments without unref()
Expand Down

0 comments on commit 9658a0a

Please sign in to comment.