Skip to content

Commit

Permalink
Implement BlockNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Jul 16, 2024
1 parent 2a36de7 commit ea28e85
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions packages/extended-sdk/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ChainAPI,
ElectionAPI,
ErrAPI,
IChainBlockInfoResponse,
IElectionListFilter,
VocdoniSDKClient,
Expand Down Expand Up @@ -36,14 +37,37 @@ export class ExtendedSDKClient extends VocdoniSDKClient {
...filters,
})
blockTransactions = (height: number, page?: number) => ChainAPI.blockTransactions(this.url, height, page)

// blockByHeight = async (height: number): Promise<IChainBlockInfoResponse | BlockNotFoundError> => {
// try {
// return await ChainAPI.blockByHeight(this.url, height)
// } catch (error) {
// if (error instanceof ErrAPI && error.raw?.response?.status === 404) {
// return { height } as BlockNotFoundError
// }
// throw error // re-throw other errors
// }
// }
blockByHeight = (height: number) => ChainAPI.blockByHeight(this.url, height)
// todo: this method will be fixed backend side, see https://github.com/vocdoni/interoperability/issues/33
blockList = (from: number, listSize: number = 10): Promise<IChainBlockInfoResponse[]> => {
const promises: Promise<IChainBlockInfoResponse>[] = []

blockList = (from: number, listSize: number = 10): Promise<Array<IChainBlockInfoResponse | BlockNotFoundError>> => {
const promises: Promise<IChainBlockInfoResponse | BlockNotFoundError>[] = []
// If is not a number bigger than 0
if (isNaN(from)) return Promise.all(promises)
for (let i = 0; i < listSize; i++) {
if (from + i > 0) promises.push(this.blockByHeight(from + i))
if (from + i > 0)
promises.push(
(async () => {
try {
return await this.blockByHeight(from + i)
} catch (error) {
if (error instanceof ErrAPI && error.raw?.response?.status === 404) {
return { height: from + i } as BlockNotFoundError
}
throw error // re-throw other errors
}
})()
)
}
return Promise.all(promises).then((blockInfo) => {
// flatten the array[][] into array[]
Expand All @@ -56,3 +80,7 @@ export class ExtendedSDKClient extends VocdoniSDKClient {
return ChainAPI.blockToDate(this.url, height)
}
}

export type BlockNotFoundError = {
height: number
}

0 comments on commit ea28e85

Please sign in to comment.