From 26c30a3c4a1af59bb1a55eff0cf50ff1b7e056a6 Mon Sep 17 00:00:00 2001 From: Martii Date: Fri, 16 Apr 2021 22:44:13 -0600 Subject: [PATCH] More error checking and casing * 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 --- controllers/user.js | 96 +++++++++++++++++++++++++++++++++++---------- libs/repoManager.js | 2 +- 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index c7a7235d7..186639679 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -1251,12 +1251,29 @@ exports.userGitHubRepoListPage = function (aReq, aRes, aNext) { } function asyncComplete(aErr) { + var msg = null; + if (aErr) { - console.error(aErr); - statusCodePage(aReq, aRes, aNext, { - statusCode: 500, - statusMessage: 'Server Error' - }); + switch (aErr.code) { + case 403: + try { + msg = JSON.parse(aErr.message); + } catch (aE) { + msg = { message: aErr.message }; + } + console.warn(msg.message); + statusCodePage(aReq, aRes, aNext, { + statusCode: 503, + statusMessage: 'Service unavailable. Please check back later.' + }); + break; + default: + console.error(aErr); + statusCodePage(aReq, aRes, aNext, { + statusCode: 500, + statusMessage: 'Server Error' + }); + } return; } @@ -1378,8 +1395,29 @@ exports.userGitHubRepoPage = function (aReq, aRes, aNext) { } function asyncComplete(aErr) { + var msg = null; + if (aErr) { - aNext(); + switch (aErr.code) { + case 403: + try { + msg = JSON.parse(aErr.message); + } catch (aE) { + msg = { message: aErr.message }; + } + console.warn(msg.message); + statusCodePage(aReq, aRes, aNext, { + statusCode: 503, + statusMessage: 'Service unavailable. Please check back later.' + }); + break; + default: + console.error(aErr); + statusCodePage(aReq, aRes, aNext, { + statusCode: 500, + statusMessage: 'Server Error' + }); + } return; } @@ -1411,7 +1449,7 @@ exports.userGitHubRepoPage = function (aReq, aRes, aNext) { if (process.env.DISABLE_SCRIPT_IMPORT === 'true') { statusCodePage(aReq, aRes, aNext, { statusCode: 503, - statusMessage: 'Service unavailable. Please check back later' + statusMessage: 'Service unavailable. Please check back later.' }); return; } @@ -1715,6 +1753,7 @@ exports.userGitHubImportScriptPage = function (aReq, aRes, aNext) { ], function (aErr) { var script = null; var code = null; + var msg = null; if (aErr) { code = (aErr instanceof statusError ? aErr.status.code : aErr.code); @@ -1727,20 +1766,35 @@ exports.userGitHubImportScriptPage = function (aReq, aRes, aNext) { } if (!(aErr instanceof String)) { - statusCodePage(aReq, aRes, aNext, { - statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code), - statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.message), - isCustomView: true, - statusData: { - isGHImport: true, - utf_pathname: githubPathName, - utf_pathext: githubPathExt, - user: encodeURIComponent(githubUserId), - repo: encodeURIComponent(githubRepoName), - default_branch: encodeURIComponent(githubDefaultBranch), - path: encodeURIComponent(githubBlobPath) - } - }); + switch (aErr.code) { // NOTE: Important to test for GH 403 vs potential OUJS 403 + case 403: + try { + msg = JSON.parse(aErr.message); + } catch (aE) { + msg = { message: aErr.message }; + } + console.warn(msg.message); + statusCodePage(aReq, aRes, aNext, { + statusCode: 503, + statusMessage: 'Service unavailable. Please check back later.' + }); + break; + default: + statusCodePage(aReq, aRes, aNext, { + statusCode: (aErr instanceof statusError ? aErr.status.code : aErr.code), + statusMessage: (aErr instanceof statusError ? aErr.status.message : aErr.message), + isCustomView: true, + statusData: { + isGHImport: true, + utf_pathname: githubPathName, + utf_pathext: githubPathExt, + user: encodeURIComponent(githubUserId), + repo: encodeURIComponent(githubRepoName), + default_branch: encodeURIComponent(githubDefaultBranch), + path: encodeURIComponent(githubBlobPath) + } + }); + } } else { statusCodePage(aReq, aRes, aNext, { statusCode: 500, diff --git a/libs/repoManager.js b/libs/repoManager.js index 1480e0c9d..d5d3c7923 100644 --- a/libs/repoManager.js +++ b/libs/repoManager.js @@ -106,7 +106,7 @@ function fetchJSON(aPath, aCallback) { var encodedAuth = Buffer.from(`${clientId}:${clientKey}`).toString('base64'); var opts = { headers: { - Authorization: `Basic ${encodedAuth}` + authorization: `basic ${encodedAuth}` } }; fetchRaw('api.github.com', aPath, function (aBufs) {