Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ebc0a4d

Browse files
author
Sachin Maheshwari
committed
common error topic, no aggregation needed during error publishing
1 parent 3a01c82 commit ebc0a4d

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

config/default.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ module.exports = {
3535
KAFKA_MESSAGE_ORIGINATOR: process.env.KAFKA_MESSAGE_ORIGINATOR || 'u-bahn-api',
3636

3737
// topics
38-
UBAHN_CREATE_USER_TOPIC: process.env.UBAHN_CREATE_USER_TOPIC || 'user.action.topic.create',
39-
UBAHN_UPDATE_USER_TOPIC: process.env.UBAHN_UPDATE_USER_TOPIC || 'user.action.topic.update',
40-
UBAHN_DELETE_USER_TOPIC: process.env.UBAHN_DELETE_USER_TOPIC || 'user.action.topic.delete',
38+
UBAHN_ERROR_TOPIC: process.env.UBAHN_ERROR_TOPIC || 'u-bahn.action.error',
39+
4140
UBAHN_CREATE_TOPIC: process.env.UBAHN_CREATE_TOPIC || 'u-bahn.action.create',
4241
UBAHN_UPDATE_TOPIC: process.env.UBAHN_UPDATE_TOPIC || 'u-bahn.action.update',
4342
UBAHN_DELETE_TOPIC: process.env.UBAHN_DELETE_TOPIC || 'u-bahn.action.delete',

src/common/helper.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ async function postEvent (topic, payload) {
160160
await busApiClient.postEvent(message)
161161
}
162162

163+
/**
164+
* Send error event to Kafka
165+
* @params {String} topic the topic name
166+
* @params {Object} payload the payload
167+
* @params {String} action for which operation error occurred
168+
*/
169+
async function publishError (topic, payload, action) {
170+
logger.debug(`Publish error to Kafka topic ${topic}, ${JSON.stringify(payload, null, 2)}`)
171+
_.set(payload, 'apiAction', action)
172+
const message = {
173+
topic,
174+
originator: config.KAFKA_MESSAGE_ORIGINATOR,
175+
timestamp: new Date().toISOString(),
176+
'mime-type': 'application/json',
177+
payload
178+
}
179+
await busApiClient.postEvent(message)
180+
}
181+
163182
module.exports = {
164183
validProperties,
165184
getAuthUser,
@@ -168,5 +187,6 @@ module.exports = {
168187
injectSearchMeta,
169188
getControllerMethods,
170189
getSubControllerMethods,
171-
postEvent
190+
postEvent,
191+
publishError
172192
}

src/modules/user/service.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function create (entity, auth) {
4343
return result
4444
} catch (e) {
4545
if (payload) {
46-
helper.postEvent(config.UBAHN_CREATE_USER_TOPIC, payload)
46+
helper.publishError(config.UBAHN_ERROR_TOPIC, payload, 'user.create')
4747
}
4848
throw e
4949
}
@@ -74,16 +74,14 @@ async function patch (id, entity, auth, params) {
7474
const result = await sequelize.transaction(async (t) => {
7575
const newEntity = await dbHelper.update(User, id, entity, auth, null, t)
7676
payload = newEntity.dataValues
77-
7877
await serviceHelper.patchRecordInEs(resource, newEntity.dataValues, true)
79-
8078
return newEntity
8179
})
8280

8381
return result
8482
} catch (e) {
8583
if (payload) {
86-
helper.postEvent(config.UBAHN_UPDATE_USER_TOPIC, payload)
84+
helper.publishError(config.UBAHN_ERROR_TOPIC, payload, 'user.update')
8785
}
8886
throw e
8987
}
@@ -203,7 +201,7 @@ async function beginCascadeDelete (id, params) {
203201
})
204202

205203
} catch (e) {
206-
helper.postEvent(config.UBAHN_DELETE_USER_TOPIC, payload)
204+
helper.publishError(config.UBAHN_ERROR_TOPIC, payload, 'user.delete')
207205
throw e
208206
}
209207
}

0 commit comments

Comments
 (0)