mirror of https://github.com/actions/toolkit
New Response Type
parent
b602df7c05
commit
79acd5bac4
|
@ -6,7 +6,7 @@ import * as cacheUtils from '../src/internal/cacheUtils'
|
|||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||
import * as tar from '../src/internal/tar'
|
||||
import {ITypedResponse} from '@actions/http-client/interfaces'
|
||||
import {ReserveCacheResponse} from '../src/internal/contracts'
|
||||
import {ReserveCacheResponse, ITypedResponseWithErrorMessage} from '../src/internal/contracts'
|
||||
|
||||
jest.mock('../src/internal/cacheHttpClient')
|
||||
jest.mock('../src/internal/cacheUtils')
|
||||
|
@ -60,7 +60,7 @@ test('save with large cache outputs should fail', async () => {
|
|||
const reserveCacheMock = jest
|
||||
.spyOn(cacheHttpClient, 'reserveCache')
|
||||
.mockImplementation(async () => {
|
||||
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:400, result: null, headers:{}}
|
||||
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:400, result: null, headers:{}, typeKey:"InvalidReserveCacheRequestException"}
|
||||
return response
|
||||
})
|
||||
|
||||
|
@ -86,7 +86,7 @@ test('save with reserve cache failure should fail', async () => {
|
|||
const reserveCacheMock = jest
|
||||
.spyOn(cacheHttpClient, 'reserveCache')
|
||||
.mockImplementation(async () => {
|
||||
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:500, result: null, headers:{}}
|
||||
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:500, result: null, headers:{}}
|
||||
return response
|
||||
})
|
||||
|
||||
|
|
|
@ -182,8 +182,9 @@ export async function saveCache(
|
|||
cacheSize
|
||||
})
|
||||
|
||||
if(reserveCacheResponse?.statusCode === 400){
|
||||
if(reserveCacheResponse?.statusCode === 400 && reserveCacheResponse?.typeKey === "InvalidReserveCacheRequestException"){
|
||||
throw new ReserveCacheError(
|
||||
reserveCacheResponse?.message ??
|
||||
`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
||||
)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,8 @@ import {
|
|||
InternalCacheOptions,
|
||||
CommitCacheRequest,
|
||||
ReserveCacheRequest,
|
||||
ReserveCacheResponse
|
||||
ReserveCacheResponse,
|
||||
ITypedResponseWithErrorMessage
|
||||
} from './contracts'
|
||||
import {downloadCacheHttpClient, downloadCacheStorageSDK} from './downloadUtils'
|
||||
import {
|
||||
|
@ -143,7 +144,7 @@ export async function reserveCache(
|
|||
key: string,
|
||||
paths: string[],
|
||||
options?: InternalCacheOptions
|
||||
): Promise<ITypedResponse<ReserveCacheResponse>> {
|
||||
): Promise<ITypedResponseWithErrorMessage<ReserveCacheResponse>> {
|
||||
const httpClient = createHttpClient()
|
||||
const version = getCacheVersion(paths, options?.compressionMethod)
|
||||
|
||||
|
@ -158,6 +159,7 @@ export async function reserveCache(
|
|||
reserveCacheRequest
|
||||
)
|
||||
)
|
||||
console.log(response)
|
||||
return response
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
import {CompressionMethod} from './constants'
|
||||
import {
|
||||
ITypedResponse
|
||||
} from '@actions/http-client/interfaces'
|
||||
|
||||
export interface ITypedResponseWithErrorMessage<T> extends ITypedResponse<T> {
|
||||
message?: string
|
||||
typeKey?: string
|
||||
}
|
||||
|
||||
export interface ArtifactCacheEntry {
|
||||
cacheKey?: string
|
||||
|
@ -25,3 +33,7 @@ export interface InternalCacheOptions {
|
|||
compressionMethod?: CompressionMethod
|
||||
cacheSize?: number
|
||||
}
|
||||
|
||||
export interface CommitCacheRequest {
|
||||
size: number
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import {
|
|||
ITypedResponse
|
||||
} from '@actions/http-client/interfaces'
|
||||
import {DefaultRetryDelay, DefaultRetryAttempts} from './constants'
|
||||
import {ITypedResponseWithErrorMessage} from './contracts'
|
||||
|
||||
export function isSuccessStatusCode(statusCode?: number): boolean {
|
||||
if (!statusCode) {
|
||||
|
@ -94,14 +95,14 @@ export async function retry<T>(
|
|||
|
||||
export async function retryTypedResponse<T>(
|
||||
name: string,
|
||||
method: () => Promise<ITypedResponse<T>>,
|
||||
method: () => Promise<ITypedResponseWithErrorMessage<T>>,
|
||||
maxAttempts = DefaultRetryAttempts,
|
||||
delay = DefaultRetryDelay
|
||||
): Promise<ITypedResponse<T>> {
|
||||
): Promise<ITypedResponseWithErrorMessage<T>> {
|
||||
return await retry(
|
||||
name,
|
||||
method,
|
||||
(response: ITypedResponse<T>) => response.statusCode,
|
||||
(response: ITypedResponseWithErrorMessage<T>) => response.statusCode,
|
||||
maxAttempts,
|
||||
delay,
|
||||
// If the error object contains the statusCode property, extract it and return
|
||||
|
@ -111,7 +112,9 @@ export async function retryTypedResponse<T>(
|
|||
return {
|
||||
statusCode: error.statusCode,
|
||||
result: null,
|
||||
headers: {}
|
||||
headers: {},
|
||||
message:error.message,
|
||||
typeKey:error.result.typeKey
|
||||
}
|
||||
} else {
|
||||
return undefined
|
||||
|
|
Loading…
Reference in New Issue