Skip to content

Commit 28f6d31

Browse files
committed
Handle toolcache errors with StartProxyError
1 parent 1d0f911 commit 28f6d31

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

lib/start-proxy-action.js

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy-action.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getApiDetails, getAuthorizationHeaderFor } from "./api-client";
1010
import { KnownLanguage } from "./languages";
1111
import { getActionsLogger, Logger } from "./logging";
1212
import {
13+
cacheProxy,
1314
Credential,
1415
credentialToStr,
1516
downloadProxy,
@@ -242,7 +243,8 @@ async function getProxyBinaryPath(logger: Logger): Promise<string> {
242243
);
243244
const temp = await downloadProxy(logger, proxyInfo.url, authorization);
244245
const extracted = await extractProxy(logger, temp);
245-
proxyBin = await toolcache.cacheDir(
246+
proxyBin = await cacheProxy(
247+
logger,
246248
extracted,
247249
proxyFileName,
248250
proxyInfo.version,

src/start-proxy.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,29 @@ test(
447447
await startProxyExports.extractProxy(logger, "path");
448448
},
449449
);
450+
451+
test("cacheProxy - returns file path on success", async (t) => {
452+
await withRecordingLoggerAsync(async (logger) => {
453+
const testPath = "/some/path";
454+
sinon.stub(toolcache, "cacheDir").resolves(testPath);
455+
456+
const result = await startProxyExports.cacheProxy(
457+
logger,
458+
"/other/path",
459+
"proxy",
460+
"1.0",
461+
);
462+
t.is(result, testPath);
463+
});
464+
});
465+
466+
test(
467+
"cacheProxy",
468+
wrapFailureTest,
469+
() => {
470+
sinon.stub(toolcache, "cacheDir").throws();
471+
},
472+
async (logger) => {
473+
await startProxyExports.cacheProxy(logger, "/other/path", "proxy", "1.0");
474+
},
475+
);

src/start-proxy.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ConfigurationError, getErrorMessage, isDefined } from "./util";
2424
export enum StartProxyErrorType {
2525
DownloadFailed = "Failed to download proxy archive.",
2626
ExtractionFailed = "Failed to extract proxy archive.",
27+
CacheFailed = "Failed to add proxy to toolcache",
2728
}
2829

2930
/**
@@ -453,3 +454,28 @@ export async function extractProxy(logger: Logger, archive: string) {
453454
throw new StartProxyError(StartProxyErrorType.ExtractionFailed);
454455
}
455456
}
457+
458+
/**
459+
* Attempts to store the proxy in the toolcache.
460+
*
461+
* @param logger The logger to use.
462+
* @param path The source path to add to the toolcache.
463+
* @param filename The filename of the proxy binary.
464+
* @param version The version of the proxy.
465+
* @returns The path to the directory in the toolcache.
466+
*/
467+
export async function cacheProxy(
468+
logger: Logger,
469+
path: string,
470+
filename: string,
471+
version: string,
472+
) {
473+
try {
474+
return await toolcache.cacheDir(path, filename, version);
475+
} catch (error) {
476+
logger.error(
477+
`Failed to add proxy archive from ${path} to toolcache: ${getErrorMessage(error)}`,
478+
);
479+
throw new StartProxyError(StartProxyErrorType.CacheFailed);
480+
}
481+
}

0 commit comments

Comments
 (0)