Skip to content

Commit

Permalink
fix: add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMedeiros committed Feb 21, 2024
1 parent 1092fb5 commit a4b349d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 56 deletions.
54 changes: 29 additions & 25 deletions src/domain/bot/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@ export class BotService {

// add cronjob list
addCronjobList() {
// cronjob list
const cronjobList = [
{
webhook: `${this.webhookUrl}/new-fees`,
interval: '*/10 * * * * *',
type: 'fees',
},
{
webhook: `${this.webhookUrl}/new-block`,
interval: '*/10 * * * * *',
type: 'block',
},
{
webhook: `${this.webhookUrl}/new-price`,
interval: '*/10 * * * * *',
type: 'price',
},
{
webhook: `${this.webhookUrl}/new-mempool`,
interval: '*/10 * * * * *',
type: 'mempool',
},
]
try {
// cronjob list
const cronjobList = [
{
webhook: `${this.webhookUrl}/new-fees`,
interval: '*/10 * * * * *',
type: 'fees',
},
{
webhook: `${this.webhookUrl}/new-block`,
interval: '*/10 * * * * *',
type: 'block',
},
{
webhook: `${this.webhookUrl}/new-price`,
interval: '*/10 * * * * *',
type: 'price',
},
{
webhook: `${this.webhookUrl}/new-mempool`,
interval: '*/10 * * * * *',
type: 'mempool',
},
]

// add cronjob list
this.murrayService.addCronJobs(cronjobList)
// add cronjob list
this.murrayService.addCronJobs(cronjobList)
} catch (error) {
console.log(error)
}
}
}
54 changes: 31 additions & 23 deletions src/domain/bot/repositories/murrayservice.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,20 @@ export class MurrayServiceRepository {
// Cronjobs

async addCronJobs(cronjobList): Promise<any> {
cronjobList.forEach(async ({ webhook, type, interval }) => {
const url = `${this.baseUrl}/cronjobs`
const bodyData = {
webhook: webhook,
type: type,
social: 'discord',
interval: interval,
}
await this.postData(url, bodyData)
})
try {
cronjobList.forEach(async ({ webhook, type, interval }) => {
const url = `${this.baseUrl}/cronjobs`
const bodyData = {
webhook: webhook,
type: type,
social: 'discord',
interval: interval,
}
await this.postData(url, bodyData)
})
} catch (error) {
console.log(error)
}
}

// Base
Expand All @@ -216,19 +220,23 @@ export class MurrayServiceRepository {
]

protected postData(url: string, bodyData: {}): Promise<any> {
Logger.debug(`POST ${url}`)

return lastValueFrom(
this.httpService.post(url, bodyData).pipe(
map((response: AxiosResponse<any>) => {
return response.data
}),
catchError(async () => {
Logger.error(`POST ${url}\n${JSON.stringify(bodyData, null, 2)}`)
throw this.defaultError
}),
),
)
try {
Logger.debug(`POST ${url}`)

return lastValueFrom(
this.httpService.post(url, bodyData).pipe(
map((response: AxiosResponse<any>) => {
return response.data
}),
catchError(async () => {
Logger.error(`POST ${url}\n${JSON.stringify(bodyData, null, 2)}`)
throw this.defaultError
}),
),
)
} catch (error) {
console.log(error)
}
}

protected getData(url: string): Promise<any> {
Expand Down
40 changes: 32 additions & 8 deletions src/domain/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,57 @@ export class WebhooksController {

@Post('/new-fees')
updateNewFees(@Body() feesDto: FeesBodyDto) {
this.webhooksService.updateNewFees(feesDto)
try {
this.webhooksService.updateNewFees(feesDto)
} catch (error) {
console.log(error)
}
}

@Post('/new-mempool')
updateNewMempool(@Body() mempoolDto: MempoolBodyDto) {
this.webhooksService.updateNewMempool(mempoolDto)
try {
this.webhooksService.updateNewMempool(mempoolDto)
} catch (error) {
console.log(error)
}
}

@Post('/new-block')
updateNewBlock(@Body() blockDto: BlockBodyDto) {
this.webhooksService.updateNewBlock(blockDto)
try {
this.webhooksService.updateNewBlock(blockDto)
} catch (error) {
console.log(error)
}
}

@Post('/new-price')
updateNewPrice(@Body() priceDto: PriceBodyDto) {
this.webhooksService.updateNewPrice(priceDto)
try {
this.webhooksService.updateNewPrice(priceDto)
} catch (error) {
console.log(error)
}
}

@Post('/op-return/:userId')
sendOpReturn(@Param() params: MessageParamsDto, @Body() opreturnDto: MessageResponseDto) {
const { userId } = params
this.webhooksService.sendOpReturn(userId, opreturnDto)
try {
const { userId } = params
this.webhooksService.sendOpReturn(userId, opreturnDto)
} catch (error) {
console.log(error)
}
}

@Post('/tip/:userId')
sendTip(@Param() params: MessageParamsDto, @Body() tipDto: MessageResponseDto) {
const { userId } = params
this.webhooksService.sendTip(userId, tipDto)
try {
const { userId } = params
this.webhooksService.sendTip(userId, tipDto)
} catch (error) {
console.log(error)
}
}
}

0 comments on commit a4b349d

Please sign in to comment.