Skip to content

Commit

Permalink
Return empty object if cant parse json
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Feb 7, 2017
1 parent 9b531fc commit 352b17c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/utilities/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ export default (url, options = {}) => {
fetch(url, options)
.then(response => {
if (options.onResponse) options.onResponse(response)
return response.json()

response.text()
.then(text => {
try {
resolve(JSON.parse(text))
} catch (e) {
resolve({})
}
})
.catch(function (error) {
return reject(error)
})
})
.then(json => resolve(json))
.catch(error => reject(error))
})
}
9 changes: 3 additions & 6 deletions tests/unit/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ describe('partial', () => {
method: POST
`
}
try {
await requests.login``
} catch (e) {
return expect(typeof e).to.equal('object')
}

expect(true).to.equal(false)
let response = await requests.login``
expect(typeof response).to.equal('object')
expect(Object.keys(response).length).to.equal(0)
})

it('passes partial url', async function () {
Expand Down

0 comments on commit 352b17c

Please sign in to comment.