Handle errors representing non-successful http responses in retry logic

This commit is contained in:
Dave Hadka
2020-08-17 10:48:15 -05:00
parent 9ad01e4fd3
commit 1ef26b2390
2 changed files with 84 additions and 12 deletions

View File

@@ -13,8 +13,16 @@ async function handleResponse(
fail('Retry method called too many times')
}
if (response.statusCode === 999) {
// Status codes >= 600 will throw an Error instead of returning a response object. This
// mimics the behavior of the http-client *Json methods, which throw an error on any
// non-successful status codes. Values in the 6xx, 7xx, and 8xx range are converted
// to the corresponding 3xx, 4xx, and 5xx status code.
if (response.statusCode >= 900) {
throw Error('Test Error')
} else if (response.statusCode >= 600) {
const error: any = Error('Test Error with Status Code')
error['statusCode'] = response.statusCode - 300
throw error
} else {
return Promise.resolve(response)
}
@@ -35,6 +43,31 @@ async function testRetryExpectingResult(
expect(actualResult.result).toEqual(expectedResult)
}
async function testRetryConvertingErrorToResult(
responses: TestResponse[],
expectedStatus: number,
expectedResult: string | null
): Promise<void> {
responses = responses.reverse() // Reverse responses since we pop from end
const actualResult = await retry(
'test',
async () => handleResponse(responses.pop()),
(response: TestResponse) => response.statusCode,
2,
(e: Error) => {
const error: any = e
return {
statusCode: error['statusCode'],
result: error['result']
}
}
)
expect(actualResult.statusCode).toEqual(expectedStatus)
expect(actualResult.result).toEqual(expectedResult)
}
async function testRetryExpectingError(
responses: TestResponse[]
): Promise<void> {
@@ -138,3 +171,16 @@ test('retry returns after client error', async () => {
null
)
})
test('retry converts errors to response object', async () => {
await testRetryConvertingErrorToResult(
[
{
statusCode: 709, // throw a 409 Conflict error
result: null
}
],
409,
null
)
})