Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Coppieters committed Apr 8, 2020
1 parent 1d3e2c0 commit d0878fe
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 33 deletions.
40 changes: 12 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const MAX_FUNDS_FOR_BUY = 20;

// ******************************

sync.runGenerator(function*() {
sync.runGenerator(function*() {try {
_printTitleHeader('SHARESIES');

if (HELP) {
Expand Down Expand Up @@ -93,32 +93,9 @@ sync.runGenerator(function*() {
sharesies.clearCache();
}

let sharesiesInfo;
try {
sharesiesInfo = yield sharesies.getInfo();
} catch (e) {
cprint.red("Failed to get sharesies info:");
cprint.red(e);
return;
}

let sharesiesStats;
try {
sharesiesStats = yield sharesies.getStats(user);
} catch (e) {
cprint.red("Failed to get sharesies stats:");
cprint.red(e);
return;
}

let sharesiesTransactions;
try {
sharesiesTransactions = yield sharesies.getTransactions(user);
} catch (e) {
cprint.red("Failed to get sharesies transactions:");
cprint.red(e);
return;
}
let sharesiesInfo = yield sharesies.getInfo();
let sharesiesStats = yield sharesies.getStats(user);
let sharesiesTransactions = yield sharesies.getTransactions(user);

let funds = yield sharesies.getFundsCleaned();
let marketPricesAverage = sharesies.getMarketPricesAverage(funds);
Expand Down Expand Up @@ -318,7 +295,14 @@ sync.runGenerator(function*() {
print.info(`Best value increases: ${lightBlueFn(maxInvestmentFundCodes)}`);
print.info(`Return (last 2 weeks): ${lightGreenFn('$' + investmentReturn.toFixed(2))}`);
print.info(`Daily Return (last 2 weeks): ${lightGreenFn('$' + totalReturnsPerDay.toFixed(2))}`);
});
} catch (e) {
cprint.red("Ended with errors:");
console.error(e.stack || e);
if (!e.stack) {
console.trace("Trace:");
}
return;
}});

// ******************************

Expand Down
2 changes: 1 addition & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function _set (in_key, in_value, in_expire) {
let fn = `${c_CLASS_NAME}._set`;

if (!in_expire) {
throw `${fn}: expiry not set!`;
throw new Error(`${fn}: expiry not set!`);
}

let now = (new Date()).getTime();
Expand Down
2 changes: 1 addition & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function _readHiddenLineSyncWindows (in_question, in_username) {
});
file.delete(copiedScriptPath);
if (cmdResult.hasError) {
throw cmdResult.error;
throw new Error(cmdResult.error);
}
return cmdResult.result;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/secureBlob.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function _decrypt(encrypted){
// ******************************

function _checkIsSet(in_value, in_displayName) {
if (typeof(in_value) === 'undefined') throw `${in_displayName} not set!`;
if (typeof(in_value) === 'undefined') throw new Error(`${in_displayName} not set!`);
}

// ******************************
4 changes: 2 additions & 2 deletions lib/sharesies.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ function getNormalizedValues(in_values) {
let maxValue = Math.max.apply(null, in_values);
let valueRange = maxValue - minValue;

if (minValue === maxValue) {
throw 'Cannot normalize funds if min === max';
if (valueRange === 0) {
return in_values.map(value => 0);
}
return in_values.map(value => (value - minValue) / valueRange);
}
Expand Down
1 change: 1 addition & 0 deletions lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

function runGenerator (generatorFunction) {
let next = function (err, arg) {
if (err && typeof(err) === "string") return it.throw(new Error(err));
if (err) return it.throw(err);

let result = it.next(arg);
Expand Down

0 comments on commit d0878fe

Please sign in to comment.