Skip to content

Commit 1b9063e

Browse files
authored
Merge pull request #1966 from actions/robherley/wrap-create-cache-err
cache: wrap create failures in ReserveCacheError
2 parents 662b9d9 + d096588 commit 1b9063e

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

packages/cache/__tests__/saveCacheV2.test.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ test('save with large cache outputs should fail using', async () => {
9292
expect(getCompressionMock).toHaveBeenCalledTimes(1)
9393
})
9494

95-
test('create cache entry failure', async () => {
95+
test('create cache entry failure on non-ok response', async () => {
9696
const paths = ['node_modules']
9797
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
9898
const infoLogMock = jest.spyOn(core, 'info')
9999

100100
const createCacheEntryMock = jest
101101
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
102-
.mockReturnValue(Promise.resolve({ok: false, signedUploadUrl: ''}))
102+
.mockResolvedValue({ok: false, signedUploadUrl: ''})
103103

104104
const createTarMock = jest.spyOn(tar, 'createTar')
105105
const finalizeCacheEntryMock = jest.spyOn(
@@ -109,7 +109,7 @@ test('create cache entry failure', async () => {
109109
const compression = CompressionMethod.Zstd
110110
const getCompressionMock = jest
111111
.spyOn(cacheUtils, 'getCompressionMethod')
112-
.mockReturnValueOnce(Promise.resolve(compression))
112+
.mockResolvedValueOnce(compression)
113113
const archiveFileSize = 1024
114114
jest
115115
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
@@ -133,6 +133,39 @@ test('create cache entry failure', async () => {
133133
expect(saveCacheMock).toHaveBeenCalledTimes(0)
134134
})
135135

136+
test('create cache entry fails on rejected promise', async () => {
137+
const paths = ['node_modules']
138+
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
139+
const infoLogMock = jest.spyOn(core, 'info')
140+
141+
const createCacheEntryMock = jest
142+
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
143+
.mockRejectedValue(new Error('Failed to create cache entry'))
144+
145+
const createTarMock = jest.spyOn(tar, 'createTar')
146+
const compression = CompressionMethod.Zstd
147+
const getCompressionMock = jest
148+
.spyOn(cacheUtils, 'getCompressionMethod')
149+
.mockResolvedValueOnce(compression)
150+
const archiveFileSize = 1024
151+
jest
152+
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
153+
.mockReturnValueOnce(archiveFileSize)
154+
155+
const cacheId = await saveCache(paths, key)
156+
expect(cacheId).toBe(-1)
157+
expect(infoLogMock).toHaveBeenCalledWith(
158+
`Failed to save: Unable to reserve cache with key ${key}, another job may be creating this cache.`
159+
)
160+
161+
expect(createCacheEntryMock).toHaveBeenCalledWith({
162+
key,
163+
version: cacheUtils.getCacheVersion(paths, compression)
164+
})
165+
expect(createTarMock).toHaveBeenCalledTimes(1)
166+
expect(getCompressionMock).toHaveBeenCalledTimes(1)
167+
})
168+
136169
test('save cache fails if a signedUploadURL was not passed', async () => {
137170
const paths = 'node_modules'
138171
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'

packages/cache/src/cache.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,16 @@ async function saveCacheV2(
525525
version
526526
}
527527

528-
const response = await twirpClient.CreateCacheEntry(request)
529-
if (!response.ok) {
528+
let signedUploadUrl
529+
530+
try {
531+
const response = await twirpClient.CreateCacheEntry(request)
532+
if (!response.ok) {
533+
throw new Error('Response was not ok')
534+
}
535+
signedUploadUrl = response.signedUploadUrl
536+
} catch (error) {
537+
core.debug(`Failed to reserve cache: ${error}`)
530538
throw new ReserveCacheError(
531539
`Unable to reserve cache with key ${key}, another job may be creating this cache.`
532540
)
@@ -536,7 +544,7 @@ async function saveCacheV2(
536544
await cacheHttpClient.saveCache(
537545
cacheId,
538546
archivePath,
539-
response.signedUploadUrl,
547+
signedUploadUrl,
540548
options
541549
)
542550

0 commit comments

Comments
 (0)