Skip to content

Commit

Permalink
Issue999-AdoptMojaloopErrorHandlingSdk (#51)
Browse files Browse the repository at this point in the history
* Issue999-AdoptMojaloopErroHandlingSdk
-reformat generic throw err for throw FSPIOPError
-add validation and internal server error FSPIOPError at database.js

* Issue999-RemoveExtraErrorHandlingImportAndUpdateRepoVersion
  • Loading branch information
gibaros authored and Sam committed Oct 4, 2019
1 parent 2bdd545 commit 0c5f7ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/data/cachedDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
const util = require('util')
const Database = require('./database.js')
const Cache = require('memory-cache').Cache
const ErrorHandler = require('@mojaloop/central-services-error-handling')

const DEFAULT_TTL_SECONDS = 60

Expand Down Expand Up @@ -112,7 +113,7 @@ class CachedDatabase extends Database {
return value
} catch (err) {
this.writeLog(`Error in getCacheValue: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down
85 changes: 43 additions & 42 deletions src/data/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
const util = require('util')
const Knex = require('knex')
const Logger = require('@mojaloop/central-services-logger')
const ErrorHandler = require('@mojaloop/central-services-error-handling')
const MLNumber = require('@mojaloop/ml-number')

/**
Expand Down Expand Up @@ -103,7 +104,7 @@ class Database {
return rows.map(r => JSON.parse(r.rule))
} catch (err) {
this.writeLog(`Error in getTransferRules: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -120,12 +121,12 @@ class Database {

if ((!rows) || rows.length < 1) {
// initiatorType does not exist, this is an error
throw new Error(`Unsupported initiatorType '${initiatorType}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported initiatorType '${initiatorType}'`)
}
return rows[0].transactionInitiatorTypeId
} catch (err) {
this.writeLog(`Error in getInitiatorType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -142,12 +143,12 @@ class Database {

if ((!rows) || rows.length < 1) {
// initiator does not exist, this is an error
throw new Error(`Unsupported initiator '${initiator}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported initiator '${initiator}'`)
}
return rows[0].transactionInitiatorId
} catch (err) {
this.writeLog(`Error in getInitiator: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -169,7 +170,7 @@ class Database {
return rows[0].transactionScenarioId
} catch (err) {
this.writeLog(`Error in getScenario: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -186,12 +187,12 @@ class Database {

if ((!rows) || rows.length < 1) {
// sub-scenario does not exist, this is an error
throw new Error(`Unsupported transaction sub-scenario '${subScenario}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported transaction sub-scenario '${subScenario}'`)
}
return rows[0].transactionSubScenarioId
} catch (err) {
this.writeLog(`Error in getSubScenario: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -208,12 +209,12 @@ class Database {

if ((!rows) || rows.length < 1) {
// amount type does not exist, this is an error
throw new Error(`Unsupported amount type '${amountType}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported amount type '${amountType}'`)
}
return rows[0].amountTypeId
} catch (err) {
this.writeLog(`Error in getAmountType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -235,7 +236,7 @@ class Database {
return transactionReferenceId
} catch (err) {
this.writeLog(`Error in createTransactionReference: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -257,7 +258,7 @@ class Database {
return quoteId
} catch (err) {
this.writeLog(`Error in createQuoteDuplicateCheck: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -280,7 +281,7 @@ class Database {
return quoteId
} catch (err) {
this.writeLog(`Error in createQuoteUpdateDuplicateCheck: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -297,13 +298,13 @@ class Database {

if ((!rows) || rows.length < 1) {
// party type does not exist, this is an error
throw new Error(`Unsupported party type '${partyType}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported party type '${partyType}'`)
}

return rows[0].partyTypeId
} catch (err) {
this.writeLog(`Error in getPartyType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -320,13 +321,13 @@ class Database {

if ((!rows) || rows.length < 1) {
// identifier type does not exist, this is an error
throw new Error(`Unsupported party identifier type '${partyIdentifierType}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported party identifier type '${partyIdentifierType}'`)
}

return rows[0].partyIdentifierTypeId
} catch (err) {
this.writeLog(`Error in getPartyIdentifierType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -346,13 +347,13 @@ class Database {

if ((!rows) || rows.length < 1) {
// active participant does not exist, this is an error
throw new Error(`Unsupported participant '${participantName}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported participant '${participantName}'`)
}

return rows[0].participantId
} catch (err) {
this.writeLog(`Error in getPartyIdentifierType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -372,13 +373,13 @@ class Database {

if ((!rows) || rows.length < 1) {
// active role type does not exist, this is an error
throw new Error(`Unsupported transfer participant role type '${name}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported transfer participant role type '${name}'`)
}

return rows[0].transferParticipantRoleTypeId
} catch (err) {
this.writeLog(`Error in getTransferParticipantRoleType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -398,13 +399,13 @@ class Database {

if ((!rows) || rows.length < 1) {
// active ledger entry type does not exist, this is an error
throw new Error(`Unsupported ledger entry type '${name}'`)
throw ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR, `Unsupported ledger entry type '${name}'`)
}

return rows[0].ledgerEntryTypeId
} catch (err) {
this.writeLog(`Error in getLedgerEntryType: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down Expand Up @@ -500,7 +501,7 @@ class Database {
return quotePartyId
} catch (err) {
this.writeLog(`Error in createQuoteParty: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -521,7 +522,7 @@ class Database {
return rows
} catch (err) {
this.writeLog(`Error in getQuotePartyView: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -543,13 +544,13 @@ class Database {
}

if (rows.length > 1) {
throw new Error(`Expected 1 row for quoteId ${quoteId} but got: ${util.inspect(rows)}`)
throw ErrorHandler.Factory.createInternalServerFSPIOPError(`Expected 1 row for quoteId ${quoteId} but got: ${util.inspect(rows)}`)
}

return rows[0]
} catch (err) {
this.writeLog(`Error in getQuoteView: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -571,13 +572,13 @@ class Database {
}

if (rows.length > 1) {
throw new Error(`Expected 1 row for quoteId ${quoteId} but got: ${util.inspect(rows)}`)
throw ErrorHandler.Factory.createInternalServerFSPIOPError(`Expected 1 row for quoteId ${quoteId} but got: ${util.inspect(rows)}`)
}

return rows[0]
} catch (err) {
this.writeLog(`Error in getQuoteResponseView: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -601,7 +602,7 @@ class Database {
return newParty
} catch (err) {
this.writeLog(`Error in createParty: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down Expand Up @@ -634,7 +635,7 @@ class Database {
return quote.quoteId
} catch (err) {
this.writeLog(`Error in createQuote: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -656,13 +657,13 @@ class Database {
}

if (rows.length > 1) {
throw new Error(`Expected 1 quoteParty row for quoteId ${quoteId} and partyType ${partyType} but got: ${util.inspect(rows)}`)
throw ErrorHandler.Factory.createInternalServerFSPIOPError(`Expected 1 quoteParty row for quoteId ${quoteId} and partyType ${partyType} but got: ${util.inspect(rows)}`)
}

return rows[0]
} catch (err) {
this.writeLog(`Error in getQuoteParty: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down Expand Up @@ -690,7 +691,7 @@ class Database {
return rows[0].value
} catch (err) {
this.writeLog(`Error in getQuotePartyEndpoint: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -715,7 +716,7 @@ class Database {
return rows[0].value
} catch (err) {
this.writeLog(`Error in getParticipantEndpoint: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -739,7 +740,7 @@ class Database {
return rows[0]
} catch (err) {
this.writeLog(`Error in getQuoteDuplicateCheck: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -763,7 +764,7 @@ class Database {
return rows[0]
} catch (err) {
this.writeLog(`Error in getQuoteResponseDuplicateCheck: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -787,7 +788,7 @@ class Database {
return rows[0]
} catch (err) {
this.writeLog(`Error in getTransactionReference: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down Expand Up @@ -823,7 +824,7 @@ class Database {
return newQuoteResponse
} catch (err) {
this.writeLog(`Error in createQuoteResponse: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -847,7 +848,7 @@ class Database {
return res
} catch (err) {
this.writeLog(`Error in createIlpPacket: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -874,7 +875,7 @@ class Database {
return res
} catch (err) {
this.writeLog(`Error in createGeoCode: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand All @@ -901,7 +902,7 @@ class Database {
return res
} catch (err) {
this.writeLog(`Error in createQuoteError: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const initServer = async function (db, config) {
validate: {
failAction: async (request, h, err) => {
Logger.error(`validation failure: ${err.stack || util.inspect(err)}`)
throw err
throw ErrorHandler.Factory.reformatFSPIOPError(err)
}
}
}
Expand Down

0 comments on commit 0c5f7ba

Please sign in to comment.