Skip to content

Commit

Permalink
StatusError fixes:
Browse files Browse the repository at this point in the history
* set name property to StatusError - mikeal#105
* pass on the actual statusMessage from the response instead of 'Incorrect statusCode ...' - mikeal#106
  • Loading branch information
tomas-optus committed May 28, 2020
1 parent d238da9 commit 14d306c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class StatusError extends Error {
Error.captureStackTrace(this, StatusError)
}

this.message = `Incorrect statusCode: ${res.status}`
this.name = 'StatusError'
this.message = res.statusMessage
this.statusCode = res.status
this.res = res
this.json = res.json.bind(res)
Expand Down
3 changes: 2 additions & 1 deletion src/nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class StatusError extends Error {
super(...params)

Error.captureStackTrace(this, StatusError)
this.message = `Incorrect statusCode: ${res.statusCode}`
this.name = 'StatusError'
this.message = res.statusMessage
this.statusCode = res.statusCode
this.json = res.json
this.text = res.text
Expand Down
113 changes: 79 additions & 34 deletions test/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,37 @@ test('base PUT string', async () => {
}
})

test('status 201', async () => {
const request = bent('string', 201)
const str = await request(u('/echo.js?statusCode=201&body=ok'))
same(str, 'ok')

try {
await request(u('/echo.js?body=ok'))
throw new Error('Call should have thrown.')
} catch (e) {
same(e.message, 'Incorrect statusCode: 200')
// basic header test
same(e.headers['content-length'], '2')
}
})
if (process.browser) {
test('status 201', async () => {
const request = bent('string', 201)
const str = await request(u('/echo.js?statusCode=201&body=ok'))
same(str, 'ok')

try {
await request(u('/echo.js?body=ok'))
throw new Error('Call should have thrown.')
} catch (e) {
same(e.message, null)
// basic header test
same(e.headers['content-length'], '2')
}
})
} else {
test('status 201', async () => {
const request = bent('string', 201)
const str = await request(u('/echo.js?statusCode=201&body=ok'))
same(str, 'ok')

try {
await request(u('/echo.js?body=ok'))
throw new Error('Call should have thrown.')
} catch (e) {
same(e.message, 'OK')
// basic header test
same(e.headers['content-length'], '2')
}
})
}

test('PUT stream', async () => {
const body = Buffer.from(Math.random().toString())
Expand Down Expand Up @@ -146,27 +163,55 @@ test('PUT JSON', async () => {
same(info.headers['content-type'], 'application/json')
})

test('500 Response body', async () => {
const request = bent()
let body
let _e
try {
await request(u('/echo.js?statusCode=500&body=ok'))
} catch (e) {
_e = e
body = e.responseBody
}
const validate = buffer => {
if (process.browser) {
same(decode(buffer), 'ok')
} else {
same(buffer.toString(), 'ok')
if (process.browser) {
test('500 Response body and message', async () => {
const request = bent()
let body
let _e
try {
await request(u('/echo.js?statusCode=500&body=ok'))
} catch (e) {
_e = e
body = e.responseBody
}
}
validate(await body)
// should be able to access again
validate(await _e.responseBody)
})
const validate = buffer => {
if (process.browser) {
same(decode(buffer), 'ok')
} else {
same(buffer.toString(), 'ok')
}
}
validate(await body)
// should be able to access again
validate(await _e.responseBody)

same(_e.message, null)
})
} else {
test('500 Response body and message', async () => {
const request = bent()
let body
let _e
try {
await request(u('/echo.js?statusCode=500&body=ok'))
} catch (e) {
_e = e
body = e.responseBody
}
const validate = buffer => {
if (process.browser) {
same(decode(buffer), 'ok')
} else {
same(buffer.toString(), 'ok')
}
}
validate(await body)
// should be able to access again
validate(await _e.responseBody)

same(_e.message, 'Internal Server Error')
})
}

test('auth', async () => {
const request = bent('https://test:[email protected]/basic-auth/test/pass', 'json')
Expand Down

0 comments on commit 14d306c

Please sign in to comment.