How to use the @actions/tool-cache.HTTPError function in @actions/tool-cache

To help you get started, we’ve selected a few @actions/tool-cache examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github actions / setup-node / src / installer.ts View on Github external
let tempDownloadFolder: string =
    'temp_' + Math.floor(Math.random() * 2000000000);
  let tempDir: string = path.join(tempDirectory, tempDownloadFolder);
  await io.mkdirP(tempDir);
  let exeUrl: string;
  let libUrl: string;
  try {
    exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`;
    libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`;

    const exePath = await tc.downloadTool(exeUrl);
    await io.cp(exePath, path.join(tempDir, 'node.exe'));
    const libPath = await tc.downloadTool(libUrl);
    await io.cp(libPath, path.join(tempDir, 'node.lib'));
  } catch (err) {
    if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
      exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
      libUrl = `https://nodejs.org/dist/v${version}/node.lib`;

      const exePath = await tc.downloadTool(exeUrl);
      await io.cp(exePath, path.join(tempDir, 'node.exe'));
      const libPath = await tc.downloadTool(libUrl);
      await io.cp(libPath, path.join(tempDir, 'node.lib'));
    } else {
      throw err;
    }
  }
  return await tc.cacheDir(tempDir, 'node', version);
}