mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-04-05 07:53:15 +08:00
Handle errors representing non-successful http responses in retry logic
This commit is contained in:
48
packages/cache/__tests__/requestUtils.test.ts
vendored
48
packages/cache/__tests__/requestUtils.test.ts
vendored
@@ -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
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user