Skip to content

Commit 0097908

Browse files
committed
Subclass the Error object
* 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
1 parent a0c6d7a commit 0097908

File tree

3 files changed

+242
-59
lines changed

3 files changed

+242
-59
lines changed

controllers/scriptStorage.js

+128-33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var isPro = require('../libs/debug').isPro;
55
var isDev = require('../libs/debug').isDev;
66
var isDbg = require('../libs/debug').isDbg;
7+
var statusError = require('../libs/debug').statusError;
78

89
//
910

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

11831184

1184-
if (!aMeta || process.env.READ_ONLY_SCRIPT_STORAGE === 'true') {
1185-
aCallback(null);
1185+
if (process.env.READ_ONLY_SCRIPT_STORAGE === 'true') {
1186+
aCallback(new statusError({
1187+
message: 'Read only script storage. Please try again later.',
1188+
code: 501 // Not Implemented
1189+
}), null);
11861190
return;
11871191
}
11881192

1193+
if (!aMeta) {
1194+
aCallback(new statusError({
1195+
message: 'Metadata block(s) missing.',
1196+
code: 400
1197+
}), null);
1198+
return;
1199+
}
11891200

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

11971208
// Can't install a script without a @name (maybe replace with random value)
11981209
if (!name) {
1199-
aCallback(null);
1210+
aCallback(new statusError({
1211+
message: '`@name` missing.',
1212+
code: 400
1213+
}), null);
12001214
return;
12011215
}
12021216

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

1210-
// Can't install a script without a cleaned @name (maybe replace with random value)
1224+
// Check for non-localized presence
12111225
if (!scriptName) {
1212-
aCallback(null);
1226+
aCallback(new statusError({
1227+
message: '`@name` non-localized missing.',
1228+
code: 400
1229+
}), null);
12131230
return;
12141231
}
12151232

12161233
// Can't install a script name ending in a reserved extension
12171234
if (/\.(?:min|user|user\.js|meta)$/.test(scriptName)) {
1218-
aCallback(null);
1235+
aCallback(new statusError({
1236+
message: '`@name` ends in a reserved extension.',
1237+
code: 400
1238+
}), null);
12191239
return;
12201240
}
12211241

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

12271247
if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
12281248
// Keysets do not match exactly... reject
1229-
aCallback(null);
1249+
aCallback(new statusError({
1250+
message: '`@name` must match in UserScript and UserLibrary metadata blocks.',
1251+
code: 400
1252+
}), null);
12301253
return;
12311254
}
12321255

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

12381261
if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
12391262
// Keysets do not match exactly... reject
1240-
aCallback(null);
1263+
aCallback(new statusError({
1264+
message: '`@description` must match in UserScript and UserLibrary metadata blocks.',
1265+
code: 400
1266+
}), null);
12411267
return;
12421268
}
12431269

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

12491275
if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
12501276
// Keysets do not match exactly... reject
1251-
aCallback(null);
1277+
aCallback(new statusError({
1278+
message: '`@copyright` must match in UserScript and UserLibrary metadata blocks.',
1279+
code: 400
1280+
}), null);
12521281
return;
12531282
}
12541283

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

12601289
if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
12611290
// Keysets do not match exactly... reject
1262-
aCallback(null);
1291+
aCallback(new statusError({
1292+
message: '`@license` must match in UserScript and UserLibrary metadata blocks.',
1293+
code: 400
1294+
}), null);
12631295
return;
12641296
}
12651297

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

12801315
for (i = 0; userKey = userKeyset[i++];) {
12811316
thisKeyComponents = userKey.split('; ');
12821317
if (thisKeyComponents.length > 2) {
12831318
// Too many parts... reject
1284-
aCallback(null);
1319+
aCallback(new statusError({
1320+
message: '`@license` has too many parts in the metadata block(s).',
1321+
code: 400
1322+
}), null);
12851323
return;
12861324
}
12871325

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

12911329
// Not a web url... reject
1292-
aCallback(null);
1330+
aCallback(new statusError({
1331+
message: '`@license` type component not a web url in the metadata block(s).',
1332+
code: 400
1333+
}), null);
12931334
return;
12941335
}
12951336
}
12961337

12971338
thatSPDX = thisKeyComponents[0].replace(/\+$/, '');
12981339
if (SPDX.indexOf(thatSPDX) === -1 || blockSPDX.indexOf(thatSPDX) > -1) {
12991340
// Absent SPDX short code or blocked SPDX... reject
1300-
aCallback(null);
1341+
aCallback(new statusError({
1342+
message: '`@license` has an incompatible SPDX in the metadata block(s).',
1343+
code: 400
1344+
}), null);
13011345
return;
13021346
}
13031347
}
13041348
} else {
13051349
// No licensing... reject
1306-
aCallback(null);
1350+
aCallback(new statusError({
1351+
message: '`@license` is absent in the metadata block(s).',
1352+
code: 400
1353+
}), null);
13071354
return;
13081355
}
13091356

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

13151362
if (isLibrary && !isEqualKeyset(slaveKeyset, masterKeyset)) {
13161363
// Keysets do not match exactly... reject
1317-
aCallback(null);
1364+
aCallback(new statusError({
1365+
message: '`@version` must match in UserScript and UserLibrary metadata blocks.',
1366+
code: 400
1367+
}), null);
13181368
return;
13191369
}
13201370

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

13271377
// Not a web url... reject
1328-
aCallback(null);
1378+
aCallback(new statusError({
1379+
message: '`@supportURL` not a web url in the UserScript metadata block.',
1380+
code: 400
1381+
}), null);
13291382
return;
13301383
}
13311384
}
@@ -1338,7 +1391,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13381391
if (!isFQUrl(homepageURL)) {
13391392

13401393
// Not a web url... reject
1341-
aCallback(null);
1394+
aCallback(new statusError({
1395+
message: '`@homepageURL` not a web url',
1396+
code: 400
1397+
}), null);
13421398
return;
13431399
}
13441400
}
@@ -1351,7 +1407,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13511407
if (!isFQUrl(updateURL)) {
13521408

13531409
// Not a web url... reject
1354-
aCallback(null);
1410+
aCallback(new statusError({
1411+
message: '`@updateURL` not a web url',
1412+
code: 400
1413+
}), null);
13551414
return;
13561415
}
13571416
}
@@ -1363,7 +1422,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13631422
if (!isFQUrl(downloadURL)) {
13641423

13651424
// Not a web url... reject
1366-
aCallback(null);
1425+
aCallback(new statusError({
1426+
message: '`@downloadURL` not a web url',
1427+
code: 400
1428+
}), null);
13671429
return;
13681430
}
13691431

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

@@ -1382,7 +1447,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
13821447
/^\/(?:install|src\/scripts)\//.test(downloadURL.pathname) &&
13831448
/\.meta\.js$/.test(downloadURL.pathname))
13841449
{
1385-
aCallback(null);
1450+
aCallback(new statusError({
1451+
message: '`@downloadURL` not .user.js',
1452+
code: 400
1453+
}), null);
13861454
return;
13871455
}
13881456
}
@@ -1401,7 +1469,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
14011469
}
14021470

14031471
if (missingExcludeAll) {
1404-
aCallback(null);
1472+
aCallback(new statusError({
1473+
message: 'UserScript Metadata Block missing `@exclude *`.',
1474+
code: 400
1475+
}), null);
14051476
return;
14061477
}
14071478
}
@@ -1449,8 +1520,17 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
14491520
var script = null;
14501521
var s3 = null;
14511522

1452-
if (aRemoved || (!aScript && (aUpdate || collaboration))) {
1453-
aCallback(null);
1523+
if (aRemoved) {
1524+
aCallback(new statusError({
1525+
message: 'Script removed permanently.',
1526+
code: 403
1527+
}), null);
1528+
return;
1529+
} else if ((!aScript && (aUpdate || collaboration))) {
1530+
aCallback(new statusError({
1531+
message: 'Collaboration restricted.',
1532+
code: 403
1533+
}), null);
14541534
return;
14551535
} else if (!aScript) {
14561536
// New script
@@ -1485,7 +1565,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
14851565
JSON.stringify(findMeta(script.meta, 'OpenUserJS.collaborator.value')) !==
14861566
JSON.stringify(collaborators))) {
14871567

1488-
aCallback(null);
1568+
aCallback(new statusError({
1569+
message: 'Forbidden with collaboration',
1570+
code: 403
1571+
}), null);
14891572
return;
14901573
}
14911574
aScript.meta = aMeta;
@@ -1524,7 +1607,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
15241607
aErr.stack
15251608
);
15261609

1527-
aCallback(null);
1610+
aCallback(new statusError({
1611+
message: 'Remote storage write error',
1612+
code: 502
1613+
}), null);
15281614
return;
15291615
}
15301616

@@ -1538,7 +1624,10 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
15381624
installName + '\n' +
15391625
JSON.stringify(aErr, null, ' ')
15401626
);
1541-
aCallback(null);
1627+
aCallback(new statusError({
1628+
message: 'Database write error',
1629+
code: 502
1630+
}), null);
15421631
return;
15431632
}
15441633

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

@@ -1567,7 +1659,7 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
15671659
);
15681660
// fallthrough
15691661
}
1570-
aCallback(aScript);
1662+
aCallback(null, aScript);
15711663
});
15721664
});
15731665
} else {
@@ -1580,18 +1672,21 @@ exports.storeScript = function (aUser, aMeta, aBuf, aUpdate, aCallback) {
15801672
);
15811673
// fallthrough
15821674
}
1583-
aCallback(aScript);
1675+
aCallback(null, aScript);
15841676
});
15851677
}
15861678
} else {
1587-
aCallback(aScript);
1679+
aCallback(null, aScript);
15881680
}
15891681
});
15901682
});
15911683
} else {
15921684
// NOTE: This shouldn't happen
15931685
console.warn('S3 `new AWS.S3()` critical error');
1594-
aCallback(null);
1686+
aCallback(new statusError({
1687+
message: 'Storage critical error',
1688+
code: 500
1689+
}), null);
15951690
return;
15961691
}
15971692
});

0 commit comments

Comments
 (0)