Skip to content

Commit f8ae8b9

Browse files
swils23ipmbclaude
authored
Output a friendlier error message if there is an unexpected response from the GitHub API (#5)
* Raise error on invalid GitHub API response * Resolve missing import * Improve error handling with better validation and messages - Add comprehensive API response validation - Check HTTP status codes before parsing JSON - Provide user-friendly error messages for common issues - Add proper JSON parsing error handling - Improve network error reporting 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Peter Baumgartner <pete@lincolnloop.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent e8e7c5a commit f8ae8b9

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const core = require("@actions/core");
2+
const os = require("os");
23
const tc = require("@actions/tool-cache");
34
const https = require("https");
45
const { join } = require("path");
@@ -11,6 +12,22 @@ async function run() {
1112
// Get the download URL for the release
1213
const releaseUrl = `https://api.github.com/repos/apppackio/apppack/releases/${version}`;
1314
const data = await downloadJson(releaseUrl);
15+
16+
// Validate the API response
17+
if (!data || !data.tag_name) {
18+
throw new Error(
19+
`Failed to fetch release information for version '${version}'. ` +
20+
`Please check if the version exists or try 'latest'.`
21+
);
22+
}
23+
24+
if (!data.assets || data.assets.length === 0) {
25+
throw new Error(
26+
`No assets found for release ${data.tag_name}. ` +
27+
`This may be a draft release or the release process may have failed.`
28+
);
29+
}
30+
1431
// strip the leading "v" from the tag name
1532
version = data.tag_name.slice(1);
1633
// Determine the platform-specific asset name
@@ -60,11 +77,30 @@ async function downloadJson(url) {
6077
{ headers: { "User-Agent": "apppackio/setup-apppack" } },
6178
(res) => {
6279
let data = "";
80+
81+
// Check for non-success status codes
82+
if (res.statusCode !== 200) {
83+
if (res.statusCode === 404) {
84+
reject(new Error(`Release not found (404). Please check if the version exists.`));
85+
} else {
86+
reject(new Error(`GitHub API request failed with status ${res.statusCode}`));
87+
}
88+
return;
89+
}
90+
6391
res.on("data", (chunk) => (data += chunk));
64-
res.on("end", () => resolve(JSON.parse(data)));
92+
res.on("end", () => {
93+
try {
94+
resolve(JSON.parse(data));
95+
} catch (e) {
96+
reject(new Error(`Failed to parse GitHub API response: ${e.message}`));
97+
}
98+
});
6599
}
66100
);
67-
req.on("error", reject);
101+
req.on("error", (e) => {
102+
reject(new Error(`Network error accessing GitHub API: ${e.message}`));
103+
});
68104
});
69105
}
70106

0 commit comments

Comments
 (0)