Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More error checking and casing #1799

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 75 additions & 21 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion libs/repoManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down