Skip to content

Commit b608750

Browse files
committed
add rejectOnError
1 parent 3bb61f0 commit b608750

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

docs/helpers/REST.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Type: [object][3]
3131
* `onRequest` **[function][6]?** an async function which can update request object.
3232
* `onResponse` **[function][6]?** an async function which can update response object.
3333
* `maxUploadFileSize` **[number][4]?** set the max content file size in MB when performing api calls.
34+
* `rejectOnErrorCode` **[boolean][5]?** reject promise when status code is 4xx or 5xx.
3435

3536

3637

lib/helper/REST.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { beautify } from '../utils.js'
1717
* @prop {function} [onRequest] - an async function which can update request object.
1818
* @prop {function} [onResponse] - an async function which can update response object.
1919
* @prop {number} [maxUploadFileSize] - set the max content file size in MB when performing api calls.
20+
* @prop {boolean} [rejectOnErrorCode=false] - reject promise when status code is 4xx or 5xx.
2021
*/
2122
const config = {}
2223

@@ -102,6 +103,7 @@ class REST extends Helper {
102103
prettyPrintJson: false,
103104
onRequest: null,
104105
onResponse: null,
106+
rejectOnErrorCode: false,
105107
}
106108

107109
// Merge config with defaults
@@ -289,6 +291,9 @@ class REST extends Helper {
289291
} catch (e) {
290292
console.log('[REST] Response error. Status code:', fetchResponse.status)
291293
}
294+
if (this.options.rejectOnErrorCode) {
295+
throw new Error(`Response error. Status code: ${fetchResponse.status}`)
296+
}
292297
}
293298
} catch (err) {
294299
if (err.name === 'TimeoutError' || err.name === 'AbortError') {

test/rest/REST_test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ describe('REST', () => {
138138
e.message.should.contain('Request timed out after 100ms')
139139
}
140140
})
141+
142+
it('should throw error when status code is 4xx or 5xx and rejectOnErrorCode is true', async () => {
143+
I.options.rejectOnErrorCode = true
144+
try {
145+
await I.sendGetRequest('/not-existing-endpoint')
146+
throw new Error('Should have thrown error')
147+
} catch (e) {
148+
e.message.should.contain('Response error. Status code: 404')
149+
}
150+
})
141151
})
142152

143153
describe('JSONResponse integration', () => {
@@ -342,7 +352,7 @@ describe('REST - Form upload', () => {
342352
}
343353
})
344354

345-
it('should not show error when file size doesnt exceedes the permit', async () => {
355+
it('should not show error when file size doesnt exceeds the permit', async () => {
346356
const form = new FormData()
347357
form.append('file', fs.createReadStream(testFile))
348358

0 commit comments

Comments
 (0)