Skip to content

Commit

Permalink
feat: add headers to error instances (#97)
Browse files Browse the repository at this point in the history
fixes #92. fixes #93
  • Loading branch information
mikeal committed Apr 13, 2020
1 parent c78fb0b commit 99e7c1f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class StatusError extends Error {
return buffer
}
Object.defineProperty(this, 'responseBody', { get })
// match Node.js headers object
this.headers = {}
for (const [key, value] of res.headers.entries()) {
this.headers[key.toLowerCase()] = value
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class StatusError extends Error {
this.json = res.json
this.text = res.text
this.arrayBuffer = res.arrayBuffer
this.headers = res.headers
let buffer
const get = () => {
if (!buffer) buffer = this.arrayBuffer()
Expand Down
2 changes: 2 additions & 0 deletions test/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ test('status 201', async () => {
throw new Error('Call should have thrown.')
} catch (e) {
same(e.message, 'Incorrect statusCode: 200')
// basic header test
same(e.headers['content-length'], '2')
}
})

Expand Down

0 comments on commit 99e7c1f

Please sign in to comment.