Skip to content

Commit

Permalink
chore(sdk): error handling improve (stephenh#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Sep 5, 2022
1 parent f0658d8 commit d840bdb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions sdk/src/base-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export abstract class BaseProcessor<

protected abstract CreateBoundContractView(): TBoundContractView

public getChainId() {
return getNetwork(this.config.network).chainId.toString()
public getChainId(): number {
return getNetwork(this.config.network).chainId
}

public onEvent(
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Meter } from './meter'
import Long from 'long'

export class EthContext {
chainId: string
chainId: number
log?: Log
block?: Block
blockNumber: Long
gauges: GaugeResult[] = []
counters: CounterResult[] = []
meter: Meter

constructor(chainId: string, block?: Block, log?: Log) {
constructor(chainId: number, block?: Block, log?: Log) {
this.chainId = chainId
this.log = log
this.block = block
Expand All @@ -32,7 +32,7 @@ export class Context<
> extends EthContext {
contract: TContractBoundView

constructor(view: TContractBoundView, chainId: string, block?: Block, log?: Log) {
constructor(view: TContractBoundView, chainId: number, block?: Block, log?: Log) {
super(chainId, block, log)
view.context = this
this.contract = view
Expand Down
35 changes: 19 additions & 16 deletions sdk/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@
import { Context } from './context'
import { errors } from 'ethers'

class EthersError extends Error {
constructor(message: string, stack?: string) {
super(message)
this.stack = stack
}

toString() {
return this.message + '\n' + this.stack?.toString()
}
}

export function transformEtherError(e: Error, ctx: Context<any, any> | undefined): Error {
let msg = ''
// @ts-ignore expected error fields
if (e.code === errors.CALL_EXCEPTION) {
// @ts-ignore expected error fields
if (e.data === '0x') {
if (ctx) {
msg += [
// @ts-ignore expected error fields
"jsonrpc eth_call return '0x' (likely contract not existed): " + e.method + '(' + e.args.join(',') + ')',
'address: ' + ctx.contract.rawContract.address + ' at chain: ' + ctx.chainId,
'block: ' + ctx.blockNumber,
// @ts-ignore expected error fields
'data: ' + e.transaction.data,
].join('\n')
msg =
"jsonrpc eth_call return '0x' (likely contract not existed) at chain " +
ctx.chainId +
': ' +
JSON.stringify(e)
} else {
msg += [
// @ts-ignore expected error fields
"jsonrpc eth_call return '0x' (likely contract not existed): " + e.method + '(' + e.args.join(',') + ')',
// @ts-ignore expected error fields
'data: ' + e.transaction.data,
].join('\n')
msg = "jsonrpc eth_call return '0x' (likely contract not existed): " + JSON.stringify(e)
}
}
return { name: 'ETHERS_ERROR', message: msg }
return new EthersError(msg, e.stack)
}

if (e.name === 'ETHERS_ERROR') {
if (e instanceof EthersError) {
return e
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels
blockNumber: Long.fromNumber(ctx.log.blockNumber, true), // TODO need number type to be long
transactionIndex: ctx.log.transactionIndex,
logIndex: ctx.log.logIndex,
chainId: ctx.chainId,
chainId: ctx.chainId.toString(),
name: name,
labels: labels,
}
Expand All @@ -22,7 +22,7 @@ function GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels
blockNumber: Long.fromNumber(ctx.block.number, true),
transactionIndex: -1,
logIndex: -1,
chainId: ctx.chainId,
chainId: ctx.chainId.toString(),
name: name,
labels: labels,
}
Expand Down
11 changes: 4 additions & 7 deletions sdk/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import {

import { Empty } from './gen/google/protobuf/empty'
import Long from 'long'
import { BaseProcessor } from './base-processor'
import { BaseContract } from 'ethers'
import { BoundContractView } from './context'
import { TextDecoder } from 'util'

const DEFAULT_MAX_BLOCK = Long.ZERO
Expand All @@ -38,7 +35,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {

// map from chain id to list of processors
// private blockHandlers = new Map<string, ((block: Block) => Promise<O11yResult>)[]>()
private processorsByChainId = new Map<string, BaseProcessor<BaseContract, BoundContractView<BaseContract, any>>>()
// private processorsByChainId = new Map<string, BaseProcessor<BaseContract, BoundContractView<BaseContract, any>>>()

private started = false
private contractConfigs: ContractConfig[]
Expand All @@ -65,7 +62,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
async configure() {
this.eventHandlers = []
this.templateInstances = []
this.processorsByChainId.clear()
// this.processorsByChainId.clear()
this.contractConfigs = []

this.templateInstances = [...global.PROCESSOR_STATE.templatesInstances]
Expand All @@ -75,13 +72,13 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
// If server favor incremental update this need to change
// Start basic config for contract
const chainId = processor.getChainId()
this.processorsByChainId.set(chainId, processor)
// this.processorsByChainId.set(chainId, processor)

const contractConfig: ContractConfig = {
processorType: 'user_processor',
contract: {
name: processor.config.name,
chainId: chainId,
chainId: chainId.toString(),
address: processor.config.address,
abi: '',
},
Expand Down

0 comments on commit d840bdb

Please sign in to comment.