feat(error-handling): error notification for network failures (#94)

Signed-off-by: hoivikaj <github@hoivik.co>
This commit is contained in:
Anthony Hoivik authored and GitHub committed 2026-03-17 15:23:28 -03:00
1 parent 4959f8f18c
commit b572e07b01
7 files changed
+335 -52

No files matched your search

+126 -14
View File
@@ -34394,6 +34394,83 @@ function _unique(values) {
return Array.from(new Set(values));
}
//# sourceMappingURL=tool-cache.js.map
;// CONCATENATED MODULE: ./lib/error-utils.js
/**
* Copyright (c) OpenTofu
* SPDX-License-Identifier: MPL-2.0
*
* Normalizes errors (including AggregateError from fetch/undici) into
* a single, actionable message for GitHub Actions logs.
*/
/**
* Get a user-friendly message from any error.
* - AggregateError: flattens error.errors so network/download causes are visible.
* - Standard Error: returns message; includes cause if present (Node 16+).
*
* @param {unknown} error - Caught value (Error, AggregateError, or other).
* @returns {string} Single-line message suitable for core.setFailed().
*/
function getErrorMessage (error) {
if (error instanceof AggregateError && Array.isArray(error.errors)) {
if (error.errors.length === 0) {
return 'AggregateError (one or more operations failed)';
}
const parts = error.errors.map((e) => (e && typeof e.message === 'string' ? e.message : String(e)));
const combined = parts.join('; ');
return combined || 'AggregateError (one or more operations failed)';
}
if (error instanceof Error) {
if (error.cause instanceof Error) {
return `${error.message}: ${error.cause.message}`;
}
return error.message;
}
return String(error);
}
/**
* Get a detailed string for logging (e.g. core.debug or core.error).
* Includes stack and, for AggregateError, each nested error.
*
* @param {unknown} error - Caught value.
* @returns {string} Multi-line detail string.
*/
function getErrorDetail (error) {
const lines = [];
if (error instanceof AggregateError && Array.isArray(error.errors)) {
lines.push(`AggregateError (${error.errors.length} error(s)):`);
error.errors.forEach((e, i) => {
lines.push(` [${i + 1}] ${e instanceof Error ? e.message : String(e)}`);
if (e instanceof Error && e.stack) {
lines.push(e.stack.split('\n').map((l) => ' ' + l).join('\n'));
}
});
if (error.stack) {
lines.push('Outer stack:');
lines.push(error.stack);
}
return lines.join('\n');
}
if (error instanceof Error) {
lines.push(error.message);
if (error.stack) lines.push(error.stack);
if (error.cause) {
lines.push('Caused by:');
lines.push(getErrorDetail(error.cause));
}
return lines.join('\n');
}
return String(error);
}
;// CONCATENATED MODULE: ./lib/releases.js
/**
* Copyright (c) HashiCorp, Inc.
@@ -34404,6 +34481,7 @@ function _unique(values) {
class Build {
constructor (version, name) {
this.name = name;
@@ -34441,7 +34519,13 @@ async function fetchReleases (githubToken) {
Accept: 'application/json'
};
const resp = await http.get(url, headers);
let resp;
try {
resp = await http.get(url, headers);
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Failed to fetch OpenTofu releases from ${url}: ${cause}`);
}
if (resp.message.statusCode !== HttpCodes.OK) {
throw new Error(
@@ -34449,8 +34533,21 @@ async function fetchReleases (githubToken) {
);
}
const body = await resp.readBody();
const releasesMeta = JSON.parse(body);
let body;
try {
body = await resp.readBody();
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Failed to read releases response: ${cause}`);
}
let releasesMeta;
try {
releasesMeta = JSON.parse(body);
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Invalid releases JSON from ${url}: ${cause}`);
}
/**
* @type {Array}
@@ -34543,6 +34640,7 @@ async function getRelease (
// __dirname is not available in ES modules, so we need to construct it ourselves
const setup_tofu_dirname = (0,external_path_namespaceObject.dirname)((0,external_url_namespaceObject.fileURLToPath)(import.meta.url));
@@ -34567,7 +34665,13 @@ function mapOS (os) {
async function downloadAndExtractCLI (url) {
core_debug(`Downloading OpenTofu CLI from ${url}`);
const pathToCLIZip = await downloadTool(url);
let pathToCLIZip;
try {
pathToCLIZip = await downloadTool(url);
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Failed to download OpenTofu from ${url}: ${cause}`);
}
if (!pathToCLIZip) {
throw new Error(`Unable to download OpenTofu from ${url}`);
@@ -34576,14 +34680,19 @@ async function downloadAndExtractCLI (url) {
let pathToCLI;
core_debug('Extracting OpenTofu CLI zip file');
if ((0,external_os_namespaceObject.platform)().startsWith('win')) {
core_debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
await mv(pathToCLIZip, fixedPathToCLIZip);
core_debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await extractZip(fixedPathToCLIZip);
} else {
pathToCLI = await extractZip(pathToCLIZip);
try {
if ((0,external_os_namespaceObject.platform)().startsWith('win')) {
core_debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
await mv(pathToCLIZip, fixedPathToCLIZip);
core_debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await extractZip(fixedPathToCLIZip);
} else {
pathToCLI = await extractZip(pathToCLIZip);
}
} catch (error) {
const cause = getErrorMessage(error);
throw new Error(`Failed to extract OpenTofu archive: ${cause}`);
}
core_debug(`OpenTofu CLI path is ${pathToCLI}.`);
@@ -34745,7 +34854,7 @@ async function run () {
}
return release;
} catch (error) {
core_error(error);
core_error(getErrorMessage(error));
throw error;
}
}
@@ -34763,11 +34872,14 @@ async function run () {
(async () => {
try {
await setup_tofu();
} catch (error) {
setFailed(error.message);
const message = getErrorMessage(error);
core_debug(getErrorDetail(error));
setFailed(message);
}
})();