Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat(core/storage): improved error handling, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Apr 23, 2020
1 parent 6122156 commit 68cb6ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/blockchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import Eth from 'web3-eth'
import { EventEmitter } from 'events'
import config from 'config'

import { loggingFactory } from '../logger'
import eventsEmitterFactory, { EventsEmitterOptions, PollingOptions } from './events'

export function getEventsEmitterForService (serviceName: string, eth: Eth, contractAbi: AbiItem[]): EventEmitter {
const contractAddresses = config.get<string>(`${serviceName}.contractAddress`)
const contract = new eth.Contract(contractAbi, contractAddresses)

const logger = loggingFactory(`${serviceName}:blockchain`)
logger.info(`For listening on service '${serviceName}' using contract on address: ${contractAddresses}`)

const eventsToListen = config.get<string[]>(`${serviceName}.events`)
const eventsEmitterOptions = config.get<EventsEmitterOptions>(`${serviceName}.eventsEmitter`)
const newBlockEmitterOptions = config.get<PollingOptions>(`${serviceName}.newBlockEmitter`)
Expand Down
3 changes: 2 additions & 1 deletion src/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import eventProcessor from './storage.blockchain'
import Price from './models/price.model'
import confFactory from '../conf'
import { ethFactory } from '../blockchain'
import { errorHandler } from '../utils'

import pinningContractAbi from '@rsksmart/rif-marketplace-storage-pinning/build/contracts/PinningManager.json'

Expand All @@ -37,7 +38,7 @@ const storage: CachedService = {
// Initialize blockchain watcher
const eth = app.get('eth') as Eth
const eventsEmitter = getEventsEmitterForService('storage', eth, pinningContractAbi.abi as AbiItem[])
eventsEmitter.on('newEvent', eventProcessor)
eventsEmitter.on('newEvent', errorHandler(eventProcessor, logger))
eventsEmitter.on('error', (e: Error) => {
logger.error(`There was unknown error in Events Emitter! ${e}`)
})
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import config from 'config'

import { Config, Store, Logger } from './types'
import { isSupportedServices, SupportedServices } from './app'
import { EventData } from 'web3-eth-contract'

const readFile = promisify(readFileCb)

Expand Down Expand Up @@ -66,6 +67,12 @@ export function validateServices (args: string[]): SupportedServices[] {
return args as SupportedServices[]
}

export function errorHandler (fn: (event: EventData) => Promise<void>, logger: Logger): (event: EventData) => void {
return (event): void => {
fn(event).catch(err => logger.error(err))
}
}

export abstract class BaseCLICommand extends Command {
static flags = {
config: flags.string({
Expand Down

0 comments on commit 68cb6ce

Please sign in to comment.