Skip to content

Commit 66c9082

Browse files
authored
More error checking and casing (#1799)
* Translate GH's 403 to 503 * If/when the dep gets updated, gracefully handle GH's 403 as much as possible at authenticated and non-authenticated requests. * The *@octokit/auth-token* dep returns lower casings for `authentication` and `basic`... not sure if matching this makes a difference but today I've been getting 45 maximum on unauthenticated GH rate limiting with this. Post #1729 Applies to #1705 #37 Auto-merge
1 parent 2706c67 commit 66c9082

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

controllers/user.js

+75-21
Original file line numberDiff line numberDiff line change
@@ -1251,12 +1251,29 @@ exports.userGitHubRepoListPage = function (aReq, aRes, aNext) {
12511251
}
12521252

12531253
function asyncComplete(aErr) {
1254+
var msg = null;
1255+
12541256
if (aErr) {
1255-
console.error(aErr);
1256-
statusCodePage(aReq, aRes, aNext, {
1257-
statusCode: 500,
1258-
statusMessage: 'Server Error'
1259-
});
1257+
switch (aErr.code) {
1258+
case 403:
1259+
try {
1260+
msg = JSON.parse(aErr.message);
1261+
} catch (aE) {
1262+
msg = { message: aErr.message };
1263+
}
1264+
console.warn(msg.message);
1265+
statusCodePage(aReq, aRes, aNext, {
1266+
statusCode: 503,
1267+
statusMessage: 'Service unavailable. Please check back later.'
1268+
});
1269+
break;
1270+
default:
1271+
console.error(aErr);
1272+
statusCodePage(aReq, aRes, aNext, {
1273+
statusCode: 500,
1274+
statusMessage: 'Server Error'
1275+
});
1276+
}
12601277
return;
12611278
}
12621279

@@ -1378,8 +1395,29 @@ exports.userGitHubRepoPage = function (aReq, aRes, aNext) {
13781395
}
13791396

13801397
function asyncComplete(aErr) {
1398+
var msg = null;
1399+
13811400
if (aErr) {
1382-
aNext();
1401+
switch (aErr.code) {
1402+
case 403:
1403+
try {
1404+
msg = JSON.parse(aErr.message);
1405+
} catch (aE) {
1406+
msg = { message: aErr.message };
1407+
}
1408+
console.warn(msg.message);
1409+
statusCodePage(aReq, aRes, aNext, {
1410+
statusCode: 503,
1411+
statusMessage: 'Service unavailable. Please check back later.'
1412+
});
1413+
break;
1414+
default:
1415+
console.error(aErr);
1416+
statusCodePage(aReq, aRes, aNext, {
1417+
statusCode: 500,
1418+
statusMessage: 'Server Error'
1419+
});
1420+
}
13831421
return;
13841422
}
13851423

@@ -1411,7 +1449,7 @@ exports.userGitHubRepoPage = function (aReq, aRes, aNext) {
14111449
if (process.env.DISABLE_SCRIPT_IMPORT === 'true') {
14121450
statusCodePage(aReq, aRes, aNext, {
14131451
statusCode: 503,
1414-
statusMessage: 'Service unavailable. Please check back later'
1452+
statusMessage: 'Service unavailable. Please check back later.'
14151453
});
14161454
return;
14171455
}
@@ -1715,6 +1753,7 @@ exports.userGitHubImportScriptPage = function (aReq, aRes, aNext) {
17151753
], function (aErr) {
17161754
var script = null;
17171755
var code = null;
1756+
var msg = null;
17181757

17191758
if (aErr) {
17201759
code = (aErr instanceof statusError ? aErr.status.code : aErr.code);
@@ -1727,20 +1766,35 @@ exports.userGitHubImportScriptPage = function (aReq, aRes, aNext) {
17271766
}
17281767

17291768
if (!(aErr instanceof String)) {
1730-
statusCodePage(aReq, aRes, aNext, {
1731-
statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code),
1732-
statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.message),
1733-
isCustomView: true,
1734-
statusData: {
1735-
isGHImport: true,
1736-
utf_pathname: githubPathName,
1737-
utf_pathext: githubPathExt,
1738-
user: encodeURIComponent(githubUserId),
1739-
repo: encodeURIComponent(githubRepoName),
1740-
default_branch: encodeURIComponent(githubDefaultBranch),
1741-
path: encodeURIComponent(githubBlobPath)
1742-
}
1743-
});
1769+
switch (aErr.code) { // NOTE: Important to test for GH 403 vs potential OUJS 403
1770+
case 403:
1771+
try {
1772+
msg = JSON.parse(aErr.message);
1773+
} catch (aE) {
1774+
msg = { message: aErr.message };
1775+
}
1776+
console.warn(msg.message);
1777+
statusCodePage(aReq, aRes, aNext, {
1778+
statusCode: 503,
1779+
statusMessage: 'Service unavailable. Please check back later.'
1780+
});
1781+
break;
1782+
default:
1783+
statusCodePage(aReq, aRes, aNext, {
1784+
statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code),
1785+
statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.message),
1786+
isCustomView: true,
1787+
statusData: {
1788+
isGHImport: true,
1789+
utf_pathname: githubPathName,
1790+
utf_pathext: githubPathExt,
1791+
user: encodeURIComponent(githubUserId),
1792+
repo: encodeURIComponent(githubRepoName),
1793+
default_branch: encodeURIComponent(githubDefaultBranch),
1794+
path: encodeURIComponent(githubBlobPath)
1795+
}
1796+
});
1797+
}
17441798
} else {
17451799
statusCodePage(aReq, aRes, aNext, {
17461800
statusCode: 500,

libs/repoManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function fetchJSON(aPath, aCallback) {
106106
var encodedAuth = Buffer.from(`${clientId}:${clientKey}`).toString('base64');
107107
var opts = {
108108
headers: {
109-
Authorization: `Basic ${encodedAuth}`
109+
authorization: `basic ${encodedAuth}`
110110
}
111111
};
112112
fetchRaw('api.github.com', aPath, function (aBufs) {

0 commit comments

Comments
 (0)