Skip to content

Commit

Permalink
feat(3666): rethrow errors in api handlers; fixed some PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
geka-evk committed Dec 19, 2023
1 parent eb0ccb2 commit 97942e7
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG NODE_VERSION=lts-alpine
# export NODE_VERSION="$(cat .nvmrc)-alpine" \
# docker build \
# --build-arg NODE_VERSION=$NODE_VERSION \
# -t mojaloop/sdk-scheme-adapter:local \
# -t mojaloop/quoting-service:local \
# . \
#

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@
"@mojaloop/central-services-error-handling": "12.0.7",
"@mojaloop/central-services-health": "14.0.2",
"@mojaloop/central-services-logger": "11.2.2",
"@mojaloop/central-services-metrics": "^12.0.8",
"@mojaloop/central-services-shared": "^18.2.0",
"@mojaloop/central-services-stream": "^11.2.0",
"@mojaloop/central-services-metrics": "12.0.8",
"@mojaloop/central-services-shared": "18.2.0",
"@mojaloop/central-services-stream": "11.2.0",
"@mojaloop/event-sdk": "14.0.0",
"@mojaloop/ml-number": "11.2.3",
"@mojaloop/sdk-standard-components": "17.1.3",
"ajv": "8.12.0",
"ajv-keywords": "5.1.0",
"axios": "1.6.2",
"blipp": "4.0.2",
"commander": "^11.1.0",
"commander": "11.1.0",
"event-stream": "4.0.1",
"fast-safe-stringify": "^2.1.1",
"good-console": "8.0.0",
Expand All @@ -114,10 +114,10 @@
"audit-ci": "^6.6.1",
"eslint": "8.16.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-jest": "27.6.0",
"jest": "29.7.0",
"jest-junit": "16.0.0",
"npm-check-updates": "16.14.11",
"npm-check-updates": "16.14.12",
"nyc": "15.1.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
Expand Down
11 changes: 5 additions & 6 deletions src/api/bulkQuotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../lib/logger')
const util = require('../lib/util')
const Config = require('../lib/config')
const dto = require('../lib/dto')

Expand All @@ -60,22 +61,20 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a POST /bulkQuotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.BULK_QUOTE.POST
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.BULK_QUOTE, Events.Event.Action.POST)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
} catch (err) {
logger.error(`error in POST /bulkQuotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 202)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
}
}
21 changes: 9 additions & 12 deletions src/api/bulkQuotes/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../../lib/logger')
const util = require('../../lib/util')
const Config = require('../../lib/config')
const dto = require('../../lib/dto')

Expand All @@ -59,23 +60,21 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a GET /bulkQuotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.BULK_QUOTE.GET
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.BULK_QUOTE, Events.Event.Action.GET)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
} catch (err) {
logger.error(`error in GET /bulkQuotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 202)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
},
/**
* summary: putBulkQuotesById
Expand All @@ -92,22 +91,20 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a PUT /bulkQuotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.BULK_QUOTE.PUT
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.BULK_QUOTE, Events.Event.Action.PUT)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.OK.CODE)
} catch (err) {
logger.error(`error in PUT /bulkQuotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 200)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.OK.CODE)
}
}
13 changes: 6 additions & 7 deletions src/api/bulkQuotes/{id}/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../../../lib/logger')
const util = require('../../../lib/util')
const Config = require('../../../lib/config')
const dto = require('../../../lib/dto')

Expand All @@ -52,30 +53,28 @@ module.exports = {
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
put: async function BulkQuotesErrorById (context, request, h) {
// todo: the same as PUT /bulkQuotes
// the same as PUT /bulkQuotes
const histTimerEnd = Metrics.getHistogram(
'bulkQuotes_id_put_error',
'Process HTTP PUT /bulkQuotes/{id}/error request',
['success']
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a PUT /bulkQuotes error request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.BULK_QUOTE.PUT
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.BULK_QUOTE, Events.Event.Action.PUT)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.OK.CODE)
} catch (err) {
logger.error(`error in PUT /bulkQuotes error request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 200)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.OK.CODE)
}
}
11 changes: 5 additions & 6 deletions src/api/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../lib/logger')
const util = require('../lib/util')
const Config = require('../lib/config')
const dto = require('../lib/dto')

Expand All @@ -60,22 +61,20 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a POST /quotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.QUOTE.POST
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.QUOTE, Events.Event.Action.POST)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
} catch (err) {
logger.error(`error in POST /quotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 202)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
}
}
21 changes: 9 additions & 12 deletions src/api/quotes/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../../lib/logger')
const util = require('../../lib/util')
const Config = require('../../lib/config')
const dto = require('../../lib/dto')

Expand All @@ -60,23 +61,21 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a GET /quotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.QUOTE.GET
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.QUOTE, Events.Event.Action.GET)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
} catch (err) {
logger.error(`error in GET /quotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 202)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.ACCEPTED.CODE)
},

/**
Expand All @@ -94,22 +93,20 @@ module.exports = {
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a PUT /quotes request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.QUOTE.PUT
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.QUOTE, Events.Event.Action.PUT)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.OK.CODE)
} catch (err) {
logger.error(`error in PUT /quotes request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 200)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.OK.CODE)
}
}
13 changes: 6 additions & 7 deletions src/api/quotes/{id}/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const { Producer } = require('@mojaloop/central-services-stream').Util
const { Http, Events } = require('@mojaloop/central-services-shared').Enum

const { logger } = require('../../../lib/logger')
const util = require('../../../lib/util')
const Config = require('../../../lib/config')
const dto = require('../../../lib/dto')

Expand All @@ -53,30 +54,28 @@ module.exports = {
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
*/
put: async function QuotesByIdAndError (context, request, h) {
// todo: the same as PUT /quotes
// the same as PUT /quotes
const histTimerEnd = Metrics.getHistogram(
'quotes_id_put_error',
'Publish HTTP PUT /quotes/{id}/error request',
['success']
).startTimer()

try {
await util.auditSpan(request)
logger.debug('got a PUT /quotes error request: ', request.payload)

const { topic, config } = kafkaConfig.PRODUCER.QUOTE.PUT
const topicConfig = dto.topicConfigDto({ topicName: topic })
const message = dto.messageFromRequestDto(request, Events.Event.Type.QUOTE, Events.Event.Action.PUT)

await Producer.produceMessage(message, topicConfig, config)

histTimerEnd({ success: true })
return h.response().code(Http.ReturnCodes.OK.CODE)
} catch (err) {
logger.error(`error in PUT /quotes error request: ${err?.message}`)
// todo: think, how we should handle such error cases:
// - how to send callback ?
// - OR reply with errorCode (not 200)?
histTimerEnd({ success: false })
util.rethrowFspiopError(err)
}

return h.response().code(Http.ReturnCodes.OK.CODE)
}
}
4 changes: 2 additions & 2 deletions src/lib/dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const messageFromRequestDto = (request, type, action) => {
content: {
requestId: request.info?.id,
headers,
payload,
params, // think, if we need this field (we have id)
payload, // todo: base64 encoded
uriParams: params,
spanContext,
id,
type,
Expand Down
Loading

0 comments on commit 97942e7

Please sign in to comment.