From ea28e85a7bd7e7889a38545ae7ed48858012d475 Mon Sep 17 00:00:00 2001 From: selankon Date: Tue, 16 Jul 2024 16:02:49 +0200 Subject: [PATCH 1/2] Implement BlockNotFoundError --- packages/extended-sdk/src/client/index.ts | 36 ++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/extended-sdk/src/client/index.ts b/packages/extended-sdk/src/client/index.ts index 4b0d4bf4..a401d5fe 100644 --- a/packages/extended-sdk/src/client/index.ts +++ b/packages/extended-sdk/src/client/index.ts @@ -1,6 +1,7 @@ import { ChainAPI, ElectionAPI, + ErrAPI, IChainBlockInfoResponse, IElectionListFilter, VocdoniSDKClient, @@ -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 => { + // 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 => { - const promises: Promise[] = [] + + blockList = (from: number, listSize: number = 10): Promise> => { + const promises: Promise[] = [] // 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[] @@ -56,3 +80,7 @@ export class ExtendedSDKClient extends VocdoniSDKClient { return ChainAPI.blockToDate(this.url, height) } } + +export type BlockNotFoundError = { + height: number +} From 6160105ce89d5bd2eb3ba1ce90b9488b8723e279 Mon Sep 17 00:00:00 2001 From: selankon Date: Tue, 16 Jul 2024 16:59:45 +0200 Subject: [PATCH 2/2] Improve code --- packages/extended-sdk/src/client/index.ts | 35 +++++++++-------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/packages/extended-sdk/src/client/index.ts b/packages/extended-sdk/src/client/index.ts index a401d5fe..ba010005 100644 --- a/packages/extended-sdk/src/client/index.ts +++ b/packages/extended-sdk/src/client/index.ts @@ -38,16 +38,6 @@ export class ExtendedSDKClient extends VocdoniSDKClient { }) blockTransactions = (height: number, page?: number) => ChainAPI.blockTransactions(this.url, height, page) - // blockByHeight = async (height: number): Promise => { - // 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) blockList = (from: number, listSize: number = 10): Promise> => { @@ -55,20 +45,21 @@ export class ExtendedSDKClient extends VocdoniSDKClient { // 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( - (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 + if (from + i <= 0) continue + 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[] // @ts-ignore