Skip to content

Commit

Permalink
Merge pull request #41 from ogd-software/master
Browse files Browse the repository at this point in the history
Enable developers to read response.json() when ResponseError is being thrown
  • Loading branch information
richmolj authored Sep 9, 2019
2 parents f065d2e + ee29c75 commit 531d35d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class Request {

let json
try {
json = await response.json()
json = await response.clone().json()
} catch (e) {
if (response.status === 202) return
throw new ResponseError(response, "invalid json", e)
Expand Down
8 changes: 6 additions & 2 deletions test/integration/fetch-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,13 @@ describe("fetch middleware", () => {
.then(({ data }) => {
expect("dont get here!").to.eq(true)
})
.catch(e => {
.catch(async e => {
expect(e.message).to.eq(
"afterFetch failed; review middleware.afterFetch stack"
)
expect(e.response.status).to.eq(401)
// If fetch.clone method is not used, error for getting json is: body used already for: URL
expect(await e.response.json()).to.be.an("object")
expect(e.originalError).to.eq(ABORT_ERR)
})
})
Expand Down Expand Up @@ -392,11 +394,13 @@ describe("fetch middleware", () => {
.then(() => {
expect("dont get here!").to.eq(true)
})
.catch(e => {
.catch(async e => {
expect(e.message).to.eq(
"afterFetch failed; review middleware.afterFetch stack"
)
expect(e.response.status).to.eq(401)
// If fetch.clone method is not used, error for getting json is: body used already for: URL
expect(await e.response.json()).to.be.an("object")
expect(e.originalError).to.eq(ABORT_ERR)
})
})
Expand Down

0 comments on commit 531d35d

Please sign in to comment.