Skip to content

Commit 6bef395

Browse files
authored
Merge pull request #2 from rmottainfo/main
Add custom HTTP status handling
2 parents 15639c2 + dfe1a13 commit 6bef395

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

GithubHttpApp/fetcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function fetcher(GH_TOKEN, URL) {
2020
const response = await UrlFetchApp.fetch(pageUrl, options);
2121

2222
if (response.getResponseCode() !== 200) {
23-
throw new Error(`API request failed with status ${response.getResponseCode()}: ${response.getContentText()}`);
23+
return handleHttpStatus(response.getResponseCode());
2424
}
2525

2626
const data = JSON.parse(response.getContentText());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Handles HTTP status codes returned from a GitHub API request.
3+
*
4+
* @param {number} statusCode - The HTTP status code.
5+
* @returns {Object} An object containing `success` (boolean) and `message` properties.
6+
* @throws {Error} Throws an error if the status code is invalid or unexpected.
7+
*/
8+
function handleHttpStatus(statusCode) {
9+
switch (statusCode) {
10+
case 204:
11+
return { success: true, message: 'Operation successfully completed.' };
12+
case 304:
13+
return { success: false, message: 'Not modified.' };
14+
case 401:
15+
return { success: false, message: 'Requires authentication.' };
16+
case 403:
17+
return { success: false, message: 'Forbidden.' };
18+
case 404:
19+
return { success: false, message: 'Resource not found.' };
20+
default:
21+
throw new Error(`Invalid HTTP status code: ${statusCode}`);
22+
}
23+
}

0 commit comments

Comments
 (0)