From 79acd5bac4c159b790337d5d833bef63d4914f3d Mon Sep 17 00:00:00 2001 From: Deepak Dahiya <59823596+t-dedah@users.noreply.github.com> Date: Thu, 31 Mar 2022 09:42:59 +0000 Subject: [PATCH] New Response Type --- packages/cache/__tests__/saveCache.test.ts | 6 +++--- packages/cache/src/cache.ts | 3 ++- packages/cache/src/internal/cacheHttpClient.ts | 6 ++++-- packages/cache/src/internal/contracts.d.ts | 12 ++++++++++++ packages/cache/src/internal/requestUtils.ts | 11 +++++++---- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/cache/__tests__/saveCache.test.ts b/packages/cache/__tests__/saveCache.test.ts index 1a6771f0..eed8f23f 100644 --- a/packages/cache/__tests__/saveCache.test.ts +++ b/packages/cache/__tests__/saveCache.test.ts @@ -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 = {statusCode:400, result: null, headers:{}} + let response: ITypedResponseWithErrorMessage = {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 = {statusCode:500, result: null, headers:{}} + let response: ITypedResponseWithErrorMessage = {statusCode:500, result: null, headers:{}} return response }) diff --git a/packages/cache/src/cache.ts b/packages/cache/src/cache.ts index ae7df95b..d5b39709 100644 --- a/packages/cache/src/cache.ts +++ b/packages/cache/src/cache.ts @@ -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.` ) } diff --git a/packages/cache/src/internal/cacheHttpClient.ts b/packages/cache/src/internal/cacheHttpClient.ts index c39abba4..18ee2bc7 100644 --- a/packages/cache/src/internal/cacheHttpClient.ts +++ b/packages/cache/src/internal/cacheHttpClient.ts @@ -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> { +): Promise> { const httpClient = createHttpClient() const version = getCacheVersion(paths, options?.compressionMethod) @@ -158,6 +159,7 @@ export async function reserveCache( reserveCacheRequest ) ) + console.log(response) return response } diff --git a/packages/cache/src/internal/contracts.d.ts b/packages/cache/src/internal/contracts.d.ts index ca6ea499..d60fd447 100644 --- a/packages/cache/src/internal/contracts.d.ts +++ b/packages/cache/src/internal/contracts.d.ts @@ -1,4 +1,12 @@ import {CompressionMethod} from './constants' +import { + ITypedResponse +} from '@actions/http-client/interfaces' + +export interface ITypedResponseWithErrorMessage extends ITypedResponse { + 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 +} diff --git a/packages/cache/src/internal/requestUtils.ts b/packages/cache/src/internal/requestUtils.ts index 2a8ba93c..41244a3a 100644 --- a/packages/cache/src/internal/requestUtils.ts +++ b/packages/cache/src/internal/requestUtils.ts @@ -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( export async function retryTypedResponse( name: string, - method: () => Promise>, + method: () => Promise>, maxAttempts = DefaultRetryAttempts, delay = DefaultRetryDelay -): Promise> { +): Promise> { return await retry( name, method, - (response: ITypedResponse) => response.statusCode, + (response: ITypedResponseWithErrorMessage) => response.statusCode, maxAttempts, delay, // If the error object contains the statusCode property, extract it and return @@ -111,7 +112,9 @@ export async function retryTypedResponse( return { statusCode: error.statusCode, result: null, - headers: {} + headers: {}, + message:error.message, + typeKey:error.result.typeKey } } else { return undefined