Skip to content

Commit

Permalink
Subclass the Error object
Browse files Browse the repository at this point in the history
* Some reworking of graceful failures to indicate what they are
* This probably should have been done around the time `statusCodePage` was implemented with ES5 compatible inheritance. May move this routine to debug.js
* One comment BUG fixed
* These trapped errors are `throw`able if we choose

NOTES:
* Almost named the inherited `Error` Object as `userError` since that's what most of these end up being. ;)
* **NOT** tested yet with the webhook... have to merge first. GitHub doesn't seem to show the statusMessage so might be out of luck here. 400 is User Error 500 is Server Error *(usually... have a few Watchpoints to figure out if they are the correct code)*
* This is pass number 1 ... optimizing/tweaking will come later after this testing and additional respite... including some possible status code changes

Applies to OpenUserJS#37
  • Loading branch information
Martii committed Nov 21, 2017
1 parent a0c6d7a commit 0097908
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 59 deletions.
161 changes: 128 additions & 33 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var isPro = require('../libs/debug').isPro;
var isDev = require('../libs/debug').isDev;
var isDbg = require('../libs/debug').isDbg;
var statusError = require('../libs/debug').statusError;

//

Expand Down Expand Up @@ -1181,11 +1182,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
var libraries = [];


if (!aMeta || process.env.READ_ONLY_SCRIPT_STORAGE === 'true') {
aCallback(null);
if (process.env.READ_ONLY_SCRIPT_STORAGE === 'true') {
aCallback(new statusError({
message: 'Read only script storage. Please try again later.',
code: 501 // Not Implemented
}), null);
return;
}

if (!aMeta) {
aCallback(new statusError({
message: 'Metadata block(s) missing.',
code: 400
}), null);
return;
}

// `@name` validations
if (!isLibrary) {
Expand All @@ -1196,7 +1207,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

// Can't install a script without a @name (maybe replace with random value)
if (!name) {
aCallback(null);
aCallback(new statusError({
message: '`@name` missing.',
code: 400
}), null);
return;
}

Expand All @@ -1207,15 +1221,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
}
});

// Can't install a script without a cleaned @name (maybe replace with random value)
// Check for non-localized presence
if (!scriptName) {
aCallback(null);
aCallback(new statusError({
message: '`@name` non-localized missing.',
code: 400
}), null);
return;
}

// Can't install a script name ending in a reserved extension
if (/\.(?:min|user|user\.js|meta)$/.test(scriptName)) {
aCallback(null);
aCallback(new statusError({
message: '`@name` ends in a reserved extension.',
code: 400
}), null);
return;
}

Expand All @@ -1226,7 +1246,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
// Keysets do not match exactly... reject
aCallback(null);
aCallback(new statusError({
message: '`@name` must match in UserScript and UserLibrary metadata blocks.',
code: 400
}), null);
return;
}

Expand All @@ -1237,7 +1260,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
// Keysets do not match exactly... reject
aCallback(null);
aCallback(new statusError({
message: '`@description` must match in UserScript and UserLibrary metadata blocks.',
code: 400
}), null);
return;
}

Expand All @@ -1248,7 +1274,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
// Keysets do not match exactly... reject
aCallback(null);
aCallback(new statusError({
message: '`@copyright` must match in UserScript and UserLibrary metadata blocks.',
code: 400
}), null);
return;
}

Expand All @@ -1259,7 +1288,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
// Keysets do not match exactly... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` must match in UserScript and UserLibrary metadata blocks.',
code: 400
}), null);
return;
}

Expand All @@ -1273,37 +1305,52 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
thatSPDX = userKeyset[userKeyset.length - 1].split('; ')[0].replace(/\+$/, '');
if (SPDXOSI.indexOf(thatSPDX) === -1) {
// No valid OSI primary e.g. last key... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` is not OSI primary and compatible in the metadata block(s).',
code: 400
}), null);
return;
}

for (i = 0; userKey = userKeyset[i++];) {
thisKeyComponents = userKey.split('; ');
if (thisKeyComponents.length > 2) {
// Too many parts... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` has too many parts in the metadata block(s).',
code: 400
}), null);
return;
}

if (thisKeyComponents.length === 2) {
if (!isFQUrl(thisKeyComponents[1])) {

// Not a web url... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` type component not a web url in the metadata block(s).',
code: 400
}), null);
return;
}
}

thatSPDX = thisKeyComponents[0].replace(/\+$/, '');
if (SPDX.indexOf(thatSPDX) === -1 || blockSPDX.indexOf(thatSPDX) > -1) {
// Absent SPDX short code or blocked SPDX... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` has an incompatible SPDX in the metadata block(s).',
code: 400
}), null);
return;
}
}
} else {
// No licensing... reject
aCallback(null);
aCallback(new statusError({
message: '`@license` is absent in the metadata block(s).',
code: 400
}), null);
return;
}

Expand All @@ -1314,7 +1361,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {

if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
// Keysets do not match exactly... reject
aCallback(null);
aCallback(new statusError({
message: '`@version` must match in UserScript and UserLibrary metadata blocks.',
code: 400
}), null);
return;
}

Expand All @@ -1325,7 +1375,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (!isFQUrl(supportURL, true)) {

// Not a web url... reject
aCallback(null);
aCallback(new statusError({
message: '`@supportURL` not a web url in the UserScript metadata block.',
code: 400
}), null);
return;
}
}
Expand All @@ -1338,7 +1391,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (!isFQUrl(homepageURL)) {

// Not a web url... reject
aCallback(null);
aCallback(new statusError({
message: '`@homepageURL` not a web url',
code: 400
}), null);
return;
}
}
Expand All @@ -1351,7 +1407,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (!isFQUrl(updateURL)) {

// Not a web url... reject
aCallback(null);
aCallback(new statusError({
message: '`@updateURL` not a web url',
code: 400
}), null);
return;
}
}
Expand All @@ -1363,7 +1422,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (!isFQUrl(downloadURL)) {

// Not a web url... reject
aCallback(null);
aCallback(new statusError({
message: '`@downloadURL` not a web url',
code: 400
}), null);
return;
}

Expand All @@ -1373,7 +1435,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
if (rAnyLocalHost.test(downloadURL.host) &&
!/^\/(?:install|src\/scripts)\//.test(downloadURL.pathname))
{
aCallback(null);
aCallback(new statusError({
message: '`@downloadURL` not .user.js',
code: 400
}), null);
return;
}

Expand All @@ -1382,7 +1447,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
/^\/(?:install|src\/scripts)\//.test(downloadURL.pathname) &&
/\.meta\.js$/.test(downloadURL.pathname))
{
aCallback(null);
aCallback(new statusError({
message: '`@downloadURL` not .user.js',
code: 400
}), null);
return;
}
}
Expand All @@ -1401,7 +1469,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
}

if (missingExcludeAll) {
aCallback(null);
aCallback(new statusError({
message: 'UserScript Metadata Block missing `@exclude *`.',
code: 400
}), null);
return;
}
}
Expand Down Expand Up @@ -1449,8 +1520,17 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
var script = null;
var s3 = null;

if (aRemoved || (!aScript && (aUpdate || collaboration))) {
aCallback(null);
if (aRemoved) {
aCallback(new statusError({
message: 'Script removed permanently.',
code: 403
}), null);
return;
} else if ((!aScript && (aUpdate || collaboration))) {
aCallback(new statusError({
message: 'Collaboration restricted.',
code: 403
}), null);
return;
} else if (!aScript) {
// New script
Expand Down Expand Up @@ -1485,7 +1565,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
JSON.stringify(findMeta(script.meta, 'OpenUserJS.collaborator.value')) !==
JSON.stringify(collaborators))) {

aCallback(null);
aCallback(new statusError({
message: 'Forbidden with collaboration',
code: 403
}), null);
return;
}
aScript.meta = aMeta;
Expand Down Expand Up @@ -1524,7 +1607,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
aErr.stack
);

aCallback(null);
aCallback(new statusError({
message: 'Remote storage write error',
code: 502
}), null);
return;
}

Expand All @@ -1538,7 +1624,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
installName + '\n' +
JSON.stringify(aErr, null, ' ')
);
aCallback(null);
aCallback(new statusError({
message: 'Database write error',
code: 502
}), null);
return;
}

Expand All @@ -1554,7 +1643,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
userDoc.name + ' was NOT role elevated from User to Author with err of:\n' +
JSON.stringify(aErr, null, ' ')
);
aCallback(aScript);
aCallback(new statusError({
message: 'Database find error',
code: 502
}), aScript);
return;
}

Expand All @@ -1567,7 +1659,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
);
// fallthrough
}
aCallback(aScript);
aCallback(null, aScript);
});
});
} else {
Expand All @@ -1580,18 +1672,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
);
// fallthrough
}
aCallback(aScript);
aCallback(null, aScript);
});
}
} else {
aCallback(aScript);
aCallback(null, aScript);
}
});
});
} else {
// NOTE: This shouldn't happen
console.warn('S3 `new AWS.S3()` critical error');
aCallback(null);
aCallback(new statusError({
message: 'Storage critical error',
code: 500
}), null);
return;
}
});
Expand Down
Loading

0 comments on commit 0097908

Please sign in to comment.