fix webhook logs

This commit is contained in:
Hongbo Wu 2023-08-10 15:07:00 +08:00
parent 6acedb64c8
commit d4ac069503
2 changed files with 41 additions and 34 deletions

View file

@ -28,7 +28,7 @@ export function webhooksServiceRouter() {
try {
const data = JSON.parse(msgStr)
const { userId, type } = data
const { userId, type } = data as { userId: string; type: string }
if (!userId || !type) {
logger.info('No userId or type found in message')
res.status(400).send('Bad Request')
@ -36,7 +36,7 @@ export function webhooksServiceRouter() {
}
// example: PAGE_CREATED
const eventType = `${type as string}_${req.params.action}`.toUpperCase()
const eventType = `${type}_${req.params.action}`.toUpperCase()
const webhooks = await getRepository(Webhook)
.createQueryBuilder()
.where('user_id = :userId', { userId })
@ -46,47 +46,54 @@ export function webhooksServiceRouter() {
if (webhooks.length <= 0) {
logger.info(
'No active webhook found for user',
userId,
'and eventType',
eventType
'No active webhook found for user ' +
userId +
' and eventType ' +
eventType
)
res.status(200).send('No webhook found')
return
}
// trigger webhooks
for (const webhook of webhooks) {
const url = webhook.url
const method = webhook.method as Method
const body = JSON.stringify({
action: req.params.action,
userId,
[type]: data,
})
logger.info('triggering webhook', url)
try {
await axios.request({
url,
method,
headers: {
'Content-Type': webhook.contentType,
},
data: body,
})
} catch (error) {
if (axios.isAxiosError(error)) {
logger.error(error.response)
} else {
logger.error(error)
await Promise.all(
webhooks.map((webhook) => {
const url = webhook.url
const method = webhook.method as Method
const body = {
action: req.params.action,
userId,
[type]: data,
}
}
}
logger.info('triggering webhook', { url, method })
return axios
.request({
url,
method,
headers: {
'Content-Type': webhook.contentType,
},
data: body,
timeout: 10000, // 10s
})
.then((response) => {
logger.info('webhook triggered', response.data)
})
.catch((error) => {
if (axios.isAxiosError(error)) {
logger.info('webhook failed', error.response)
} else {
logger.info('webhook failed', error)
}
})
})
)
res.status(200).send('OK')
} catch (err) {
logger.info('trigger webhook failed', err)
logger.error('trigger webhook failed', err)
res.status(500).send(err)
}
})

View file

@ -99,7 +99,7 @@ const truncateObjectDeep = (object: any, length: number): any => {
const truncateDeep = (obj: any, level: number): any => {
// reach maximum call stack size
if (level >= 6) {
if (level >= 5) {
return obj
}