Skip to content

Commit

Permalink
fixed some exception handling. no alias, no transaction found. fees a…
Browse files Browse the repository at this point in the history
…re more robust.
  • Loading branch information
reyesrene committed Jan 27, 2022
1 parent e4a6047 commit c528868
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 72 deletions.
67 changes: 45 additions & 22 deletions config/feeConf.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
if(!process.env.REGULAR_TRANSACTION_FEE) throw new Error('Environment Variable missing: REGULAR_TRANSACTION_FEE');
if(!process.env.ACCOUNT_RECORD_FEE) throw new Error('Environment Variable missing: ACCOUNT_RECORD_FEE');
if(!process.env.NEW_USER_FUNDING_FEE) throw new Error('Environment Variable missing: NEW_USER_FUNDING_FEE');
if(!process.env.ORDINARY_PAYMENT_FEE) throw new Error('Environment Variable missing: ORDINARY_PAYMENT_FEE');
if(!process.env.ACCOUNT_PROPERTY_FEE) throw new Error('Environment Variable missing: ACCOUNT_PROPERTY_FEE');
if(!process.env.INVITATION_TO_CHANNEL_FEE) throw new Error('Environment Variable missing: INVITATION_TO_CHANNEL_FEE');
if(!process.env.METIS_CHANNEL_MEMBER_FEE) throw new Error('Environment Variable missing: METIS_CHANNEL_MEMBER_FEE');
if(!process.env.ALIAS_ASSIGNMENT_FEE) throw new Error('Environment Variable missing: ALIAS_ASSIGNMENT_FEE');
if(!process.env.ACCOUNT_PROPERTY_DELETION_FEE) throw new Error('Environment Variable missing: ACCOUNT_PROPERTY_DELETION_FEE');
if(!process.env.METIS_MESSAGE_FEE) throw new Error('Environment Variable missing: METIS_MESSAGE_FEE');
if(!process.env.MESSAGE_CHARACTER_FEE) throw new Error('Environment Variable missing: MESSAGE_CHARACTER_FEE');
// const _ = require("lodash");
if(isNaN(process.env.REGULAR_TRANSACTION_FEE)) throw new Error(`Environment Variable REGULAR_TRANSACTION_FEE is not a number ${process.env.REGULAR_TRANSACTION_FEE}`);
const regularTransactionFee = +process.env.REGULAR_TRANSACTION_FEE;
// if(!process.env.REGULAR_TRANSACTION_FEE) throw new Error('Environment Variable missing: REGULAR_TRANSACTION_FEE');
if(isNaN(process.env.ACCOUNT_RECORD_FEE)) throw new Error(`Environment Variable ACCOUNT_RECORD_FEE is not a number ${process.env.ACCOUNT_RECORD_FEE}`);
const accountRecordFee = +process.env.ACCOUNT_RECORD_FEE;
// if(!process.env.ACCOUNT_RECORD_FEE) throw new Error('Environment Variable missing: ACCOUNT_RECORD_FEE');
if(isNaN(process.env.NEW_USER_FUNDING_FEE)) throw new Error(`Environment Variable NEW_USER_FUNDING_FEE is not a number ${process.env.NEW_USER_FUNDING_FEE}`);
const newUserFundingFee = +process.env.NEW_USER_FUNDING_FEE;
// if(!process.env.NEW_USER_FUNDING_FEE) throw new Error('Environment Variable missing: NEW_USER_FUNDING_FEE');
if(isNaN(process.env.ORDINARY_PAYMENT_FEE)) throw new Error(`Environment Variable ORDINARY_PAYMENT_FEE is not a number ${process.env.ORDINARY_PAYMENT_FEE}`);
const ordinaryPaymentFee = +process.env.ORDINARY_PAYMENT_FEE;
// if(!process.env.ORDINARY_PAYMENT_FEE) throw new Error('Environment Variable missing: ORDINARY_PAYMENT_FEE');
if(isNaN(process.env.ACCOUNT_PROPERTY_FEE)) throw new Error(`Environment Variable ACCOUNT_PROPERTY_FEE is not a number ${process.env.ACCOUNT_PROPERTY_FEE}`);
const accountPropertyFee = +process.env.ACCOUNT_PROPERTY_FEE;
// if(!process.env.ACCOUNT_PROPERTY_FEE) throw new Error('Environment Variable missing: ACCOUNT_PROPERTY_FEE');
if(isNaN(process.env.INVITATION_TO_CHANNEL_FEE)) throw new Error(`Environment Variable INVITATION_TO_CHANNEL_FEE is not a number ${process.env.INVITATION_TO_CHANNEL_FEE}`);
const invitationToChannelFee = +process.env.INVITATION_TO_CHANNEL_FEE;
// if(!process.env.INVITATION_TO_CHANNEL_FEE) throw new Error('Environment Variable missing: INVITATION_TO_CHANNEL_FEE');
if(isNaN(process.env.METIS_CHANNEL_MEMBER_FEE)) throw new Error(`Environment Variable METIS_CHANNEL_MEMBER_FEE is not a number ${process.env.METIS_CHANNEL_MEMBER_FEE}`);
const metisChannelMemberFee = +process.env.METIS_CHANNEL_MEMBER_FEE;
// if(!process.env.METIS_CHANNEL_MEMBER_FEE) throw new Error('Environment Variable missing: METIS_CHANNEL_MEMBER_FEE');
if(isNaN(process.env.ALIAS_ASSIGNMENT_FEE)) throw new Error(`Environment Variable ALIAS_ASSIGNMENT_FEE is not a number ${process.env.ALIAS_ASSIGNMENT_FEE}`);
const aliasAssignmentFee = +process.env.ALIAS_ASSIGNMENT_FEE;
// if(!process.env.ALIAS_ASSIGNMENT_FEE) throw new Error('Environment Variable missing: ALIAS_ASSIGNMENT_FEE');
if(isNaN(process.env.ACCOUNT_PROPERTY_DELETION_FEE)) throw new Error(`Environment Variable ACCOUNT_PROPERTY_DELETION_FEE is not a number ${process.env.ACCOUNT_PROPERTY_DELETION_FEE}`);
const accountPropertyDeletionFee = +process.env.ACCOUNT_PROPERTY_DELETION_FEE;
// if(!process.env.ACCOUNT_PROPERTY_DELETION_FEE) throw new Error('Environment Variable missing: ACCOUNT_PROPERTY_DELETION_FEE');
if(isNaN(process.env.METIS_MESSAGE_FEE)) throw new Error(`Environment Variable METIS_MESSAGE_FEE is not a number ${process.env.METIS_MESSAGE_FEE}`);
const metisMessageFee = +process.env.METIS_MESSAGE_FEE;
if(isNaN(process.env.MESSAGE_CHARACTER_FEE)) throw new Error(`Environment Variable MESSAGE_CHARACTER_FEE is not a number ${process.env.MESSAGE_CHARACTER_FEE}`);
const messageCharacterFee = +process.env.MESSAGE_CHARACTER_FEE;
// if(!process.env.MESSAGE_CHARACTER_FEE) throw new Error('Environment Variable missing: MESSAGE_CHARACTER_FEE');

module.exports.feeConf = {
regularTransaction: {NQT: process.env.REGULAR_TRANSACTION_FEE},
accountRecord: {NQT: process.env.ACCOUNT_RECORD_FEE},
newUserFunding: {NQT: process.env.NEW_USER_FUNDING_FEE},
ordinaryPayment: {NQT: process.env.ORDINARY_PAYMENT_FEE},
accountProperty: {NQT: process.env.ACCOUNT_PROPERTY_FEE},
accountPropertyDeletion: {NQT: process.env.ACCOUNT_PROPERTY_DELETION_FEE},
invitationToChannel: {NQT: process.env.INVITATION_TO_CHANNEL_FEE},
channelMember: {NQT: process.env.METIS_CHANNEL_MEMBER_FEE},
aliasAssignment: {NQT: process.env.ALIAS_ASSIGNMENT_FEE},
message: {NQT: process.env.METIS_MESSAGE_FEE},
messageCharacter: {NQT: process.env.MESSAGE_CHARACTER_FEE}
regularTransaction: {NQT: regularTransactionFee},
accountRecord: {NQT: accountRecordFee},
newUserFunding: {NQT: newUserFundingFee},
ordinaryPayment: {NQT: ordinaryPaymentFee},
accountProperty: {NQT: accountPropertyFee},
accountPropertyDeletion: {NQT: accountPropertyDeletionFee},
invitationToChannel: {NQT: invitationToChannelFee},
channelMember: {NQT: metisChannelMemberFee},
aliasAssignment: {NQT: aliasAssignmentFee},
message: {NQT: metisMessageFee},
messageCharacter: {NQT: messageCharacterFee}
}


30 changes: 19 additions & 11 deletions errors/metisError.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ class MetisError extends Error {


class JupiterApiError extends Error {
constructor(messsage = '', httpResponseStatus = StatusCode.ServerErrorInternal) {
constructor(messsage = '', httpResponseStatus = StatusCode.ServerErrorInternal, serverErrorCode = null) {
super(`API Response Error: ${messsage}`);
this.name = `MetisApiError`;
this.status = httpResponseStatus;
this.code = MetisErrorCode.JupiterApiError;
this.serverErrorCode = serverErrorCode;
Object.setPrototypeOf(this, JupiterApiError.prototype); //fixes a problem with instanceof
}
}

class UnknownAliasError extends Error {
/**
*
* @param message
*/
constructor(message = '') {

class MetisErrorUnknownAlias extends Error {
constructor(message = '', aliasName = '') {
super(message);
this.name = `UnknownAliasError`;
this.code = MetisErrorCode.UnknownAliasError;
Object.setPrototypeOf(this, UnknownAliasError.prototype); //fixes a problem with instanceof
this.name = `MetisErrorUnknownAlias`;
this.code = MetisErrorCode.MetisErrorUnknownAlias;
this.aliasName = aliasName;
Object.setPrototypeOf(this, MetisErrorUnknownAlias.prototype); //fixes a problem with instanceof
}
}

Expand Down Expand Up @@ -294,9 +293,17 @@ class MetisErrorFileTooLarge extends MetisError {
Object.setPrototypeOf(this, MetisErrorFileTooLarge.prototype); //fixes a problem with instanceof
}
}
class MetisErrorJupiterUnknownTransaction extends MetisError {
constructor(message = '', transactionId='') {
super(`Unknown Transaction ${transactionId}. ${message}`);
this.name = "MetisErrorJupiterUnknownTransaction"
this.code = MetisErrorCode.MetisErrorJupiterUnknownTransaction;
Object.setPrototypeOf(this, MetisErrorJupiterUnknownTransaction.prototype); //fixes a problem with instanceof
}
}
module.exports.MetisError = MetisError;
module.exports.JupiterApiError = JupiterApiError;
module.exports.UnknownAliasError = UnknownAliasError;
module.exports.MetisErrorUnknownAlias = MetisErrorUnknownAlias;
// module.exports.BadJupiterAddressError = BadJupiterAddressError;
module.exports.BadGravityAccountPropertiesError = BadGravityAccountPropertiesError;
module.exports.ChannelRecordValidatorError = ChannelRecordValidatorError;
Expand Down Expand Up @@ -324,3 +331,4 @@ module.exports.MetisErrorBadRequestParams = MetisErrorBadRequestParams;
module.exports.MetisErrorPushNotificationFailed = MetisErrorPushNotificationFailed;
module.exports.MetisErrorSendMessageToJupiterFailed = MetisErrorSendMessageToJupiterFailed;
module.exports.MetisErrorFileTooLarge = MetisErrorFileTooLarge;
module.exports.MetisErrorJupiterUnknownTransaction = MetisErrorJupiterUnknownTransaction;
16 changes: 6 additions & 10 deletions services/FeeManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ class FeeManager {
const fees = this.fees.filter(fee => {
return feeType === fee.feeType
})

if (fees.length) {
return fees[0].fee
}

if (fees.length) return fees[0].fee
throw new Error('Fee doesnt exist');
}

Expand Down Expand Up @@ -173,12 +169,12 @@ class FeeManager {
* @return {number}
*/
calculateMessageFee(messageSize){
if(!_.isNumber(messageSize)){
throw new Error(`messageSize needs to be a number`);
}
const baseFee = +this.getFee(FeeManager.feeTypes.metisMessage);
logger.verbose(`#### calculateMessageFee(messageSize)`);
if(!_.isNumber(messageSize)) throw new Error(`messageSize needs to be a number`);
const baseFee = this.getFee(FeeManager.feeTypes.metisMessage);
const charFee = +this.getFee(FeeManager.feeTypes.messageCharacter);
return baseFee + (messageSize*charFee);
const calculatedFee = baseFee + (messageSize*charFee);
return calculatedFee;
}

/**
Expand Down
1 change: 0 additions & 1 deletion services/chanService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,6 @@ class ChanService {
const multiChannelRecords = await this.fetchMultiChannelRecords(transactionIds,memberProperties.crypto, memberProperties.passphrase);
const listOfChannelsAndTheirProperties = multiChannelRecords.map( async channelRecord => {
const properties = await instantiateGravityAccountProperties(channelRecord.passphrase, channelRecord.password);
console.log('#->16', channelRecord);
properties.channelName = channelRecord.channelName; //@TODO make this more robust.
properties.createdBy = channelRecord.createdBy; //@TODO make this more robust.
properties.createdAt = channelRecord.createdAt; //@TODO make this more robust.
Expand Down
38 changes: 30 additions & 8 deletions services/jupiterAPIService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ApplicationAccountProperties, metisApplicationAccountProperties} from ".
import {FeeManager, feeManagerSingleton} from "./FeeManager";
import {axiosData, axiosDefault} from "../config/axiosConf";
import {GravityAccountProperties} from "../gravity/gravityAccountProperties";
import {JupiterApiError, MetisError, UnknownAliasError} from "../errors/metisError";
import {JupiterApiError, MetisError, MetisErrorUnknownAlias} from "../errors/metisError";
import {StatusCode} from "../utils/statusCode";
import {HttpMethod} from "../utils/httpMethod";
// import {add} from "lodash";
Expand All @@ -27,6 +27,13 @@ class JupiterAPIService {
this.appProps = applicationAccountProperties;
}


// static get JupiterErrorCodes(){
// return {
// unknownTransaction: 5
// }
// }

/**
*
* @returns {{GetAccountId: string, GetBalance: string, SetAlias: string, GetUnconfirmedTransactions: string, GetAccount: string, SendMetisMessage: string, GetAccountProperties: string, SendMoney: string, ReadMessage: string, GetAliases: string, GetTransaction: string, GetBlockchainTransactions: string, GetAlias: string, DecryptFrom: string}}
Expand Down Expand Up @@ -103,14 +110,27 @@ class JupiterAPIService {
throw new JupiterApiError(response.error, StatusCode.ServerErrorInternal)
}
if (response.hasOwnProperty('data') && response.data.hasOwnProperty('errorDescription') && response.data.errorDescription) {
const serverErrorCode = response.data.errorCode;
const serverErrorDescription = response.data.errorDescription;
if(serverErrorDescription === 'Unknown alias'){
const aliasName = params.hasOwnProperty('aliasName')?params.aliasName:'';
throw new mError.MetisErrorUnknownAlias(serverErrorDescription, aliasName);
}
if(serverErrorDescription === 'Unknown Transaction'){
const transactionId = params.hasOwnProperty('transaction')?params.transaction:'';
throw new mError.MetisErrorJupiterUnknownTransaction(serverErrorDescription, transactionId);
}
logger.error(`**** jupiterRequest().then(response) response.data.errorDescription...`);
logger.error(`errorDescription= ${response.data.errorDescription}`);
logger.error(`errorCode= ${response.data.errorCode}`);
logger.sensitive(`params= ${JSON.stringify(params)}`);
throw new JupiterApiError(response.data.errorDescription, StatusCode.ServerErrorInternal)
throw new JupiterApiError(serverErrorDescription, StatusCode.ServerErrorInternal, serverErrorCode)

}
return response;
} catch(error){
if(error instanceof mError.MetisErrorUnknownAlias) throw error;
// if(error instanceof mError.MetisErrorJupiterUnknownTransaction)throw error;
logger.error(`****************************************************************`);
logger.error(`** jupiterRequest().axios.catch(error)`)
logger.error(`****************************************************************`);
Expand Down Expand Up @@ -1036,13 +1056,15 @@ class JupiterAPIService {
aliasName,
requestType: JupiterAPIService.RequestType.GetAlias,
}
return this.get(params);

return this.get(params).catch( error => {
if( error.message === 'API Response Error: Unknown alias'){
throw new UnknownAliasError('Alias is not found');
}
throw error;
})
// return this.get(params).catch( error => {
// if( error.message === 'API Response Error: Unknown alias'){
// throw new mError.MetisErrorUnknownAlias(`alias is not found.`, params.aliasName);
// // throw new UnknownAliasError('Alias is not found');
// }
// throw error;
// })
}

/**
Expand Down
20 changes: 8 additions & 12 deletions services/jupiterAccountService.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import gu from '../utils/gravityUtils';
import {channelConfig, userConfig} from '../config/constants';
import {
instantiateGravityAccountProperties,
refreshGravityAccountProperties
instantiateGravityAccountProperties
} from "../gravity/instantiateGravityAccountProperties";
import {gravityService} from "./gravityService";
import {transactionUtils} from "../gravity/transactionUtils";
import {
BadGravityAccountPropertiesError,
BadJupiterAddressError,
MetisError, MetisErrorPublicKeyExists,
UnknownAliasError
MetisError, MetisErrorPublicKeyExists
} from "../errors/metisError";
import {axiosDefault} from "../config/axiosConf";
const {FeeManager, feeManagerSingleton} = require('./FeeManager');

// metisGravityAccountProperties

const mError = require(`../errors/metisError`);
const {GravityAccountProperties, metisGravityAccountProperties, myTest} = require('../gravity/gravityAccountProperties');
const {jupiterAPIService} = require('./jupiterAPIService');
const {tableService} = require('./tableService');
const {jupiterTransactionsService} = require('./jupiterTransactionsService');
const logger = require('../utils/logger')(module);
const bcrypt = require('bcrypt-nodejs');
// const bcrypt = require('bcrypt-nodejs');

class JupiterAccountService {
constructor(jupiterAPIService, applicationProperties, tableService, jupiterTransactionsService, gravityService, transactionUtils) {
Expand Down Expand Up @@ -890,15 +884,17 @@ class JupiterAccountService {
return false;
})
.catch( error => {
if(error.name === "UnknownAliasError"){
if(error instanceof mError.MetisErrorUnknownAlias){
return true;
}
// if(error.name === "UnknownAliasError"){
// return true;
// }

logger.error(`***********************************************************************************`);
logger.error(`** isAliasAvailable(aliasName).catch(error)`);
logger.error(`** `);
logger.sensitive(`error=${error}`);

logger.error(`error=${error}`);
console.log(error);
throw error;
})
Expand Down
5 changes: 4 additions & 1 deletion services/jupiterFundingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class JupiterFundingService {
workTime += milliseconds;
return; // Continue the countdown.
}

if(error instanceof mError.MetisErrorJupiterUnknownTransaction){
workTime += milliseconds;
return; // Continue the countdown.
}
throw error;
}
}, milliseconds);
Expand Down
3 changes: 2 additions & 1 deletion src/jim/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ const uploadController = (req,res,next,app,jobs,websocket) => {
logger.error(`** /jim/v1/api/file bb.on(Close)`);
logger.error(`****************************************************************`);
console.log(error);
return res.status(StatusCode.ClientErrorBadRequest).send({message: error.message})
return abort(req, res,bb,StatusCode.ClientErrorBadRequest, error);
// return res.status(StatusCode.ClientErrorBadRequest).send({message: error.message})
}
})
req.on("aborted", ()=> abort(req, res,bb,StatusCode.ServerErrorInternal, new mError.MetisError('aborted')));
Expand Down
5 changes: 0 additions & 5 deletions src/jim/services/storageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,6 @@ class StorageService {
const allChunkTransactionsInfo = this.transactionUtils.extractTransactionInfoFromTransactionResponses(sendMessageResponses,['transactionId','nonce']);
allChunkTransactionsData = await allChunkTransactionsInfo.reduce(async (reduced, tInfo) => {
const sharedKey = await this.jupiterTransactionsService.getSharedKey(toAccountProperties.address , toAccountProperties.passphrase, tInfo.nonce);
console.log(`\n`);
console.log('=-=-=-=-=-=-=-=-=-=-=-=-= _REMOVEME =-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-')
console.log(`sharedKey:`);
console.log(sharedKey);
console.log(`=-=-=-=-=-=-=-=-=-=-=-=-= REMOVEME_ =-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-\n`)
const data = await reduced;
return [...data, { ...tInfo, sharedKey } ];
}, Promise.resolve([]));
Expand Down
3 changes: 2 additions & 1 deletion utils/metisErrorCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module.exports.MetisErrorCode = {
MetisError: 16000,
JupiterApiError: 16001,
UnknownAliasError: 16002,
MetisErrorUnknownAlias: 16002,
MetisErrorNoChannelAccountFound: 16003,
BadGravityAccountPropertiesError: 16004,
ChannelRecordValidatorError: 16005,
Expand All @@ -35,4 +35,5 @@ module.exports.MetisErrorCode = {
MetisErrorPushNotificationFailed: 16027,
MetisErrorSendMessageToJupiterFailed: 16028,
MetisErrorFileTooLarge: 16029,
MetisErrorJupiterUnknownTransaction: 16030,
}

0 comments on commit c528868

Please sign in to comment.