From 9ed415559f7310731a6e4590702a203acebc07c8 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Fri, 8 Sep 2023 14:30:05 +0900 Subject: [PATCH 1/2] add try-catch block to handle parsing errors --- src/PythConnection.ts | 48 +++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/PythConnection.ts b/src/PythConnection.ts index eafe6ce..877d84a 100644 --- a/src/PythConnection.ts +++ b/src/PythConnection.ts @@ -1,5 +1,5 @@ -import { Connection, PublicKey, Commitment, AccountInfo } from '@solana/web3.js' -import { parseBaseData, parsePriceData, parseProductData, PriceData, Product, ProductData, AccountType } from './index' +import { AccountInfo, Commitment, Connection, PublicKey } from '@solana/web3.js' +import { AccountType, PriceData, Product, ProductData, parseBaseData, parsePriceData, parseProductData } from './index' const ONES = '11111111111111111111111111111111' @@ -78,27 +78,31 @@ export class PythConnection { } private handleAccount(key: PublicKey, account: AccountInfo, productOnly: boolean, slot: number) { - const base = parseBaseData(account.data) - // The pyth program owns accounts that don't contain pyth data, which we can safely ignore. - if (base) { - switch (base.type) { - case AccountType.Mapping: - // We can skip these because we're going to get every account owned by this program anyway. - break - case AccountType.Product: - this.handleProductAccount(key, account, slot) - break - case AccountType.Price: - if (!productOnly) { - this.handlePriceAccount(key, account, slot) - } - break - case AccountType.Test: - case AccountType.Permission: - break - default: - throw new Error(`Unknown account type: ${base.type}. Try upgrading pyth-client.`) + try { + const base = parseBaseData(account.data) + // The pyth program owns accounts that don't contain pyth data, which we can safely ignore. + if (base) { + switch (base.type) { + case AccountType.Mapping: + // We can skip these because we're going to get every account owned by this program anyway. + break + case AccountType.Product: + this.handleProductAccount(key, account, slot) + break + case AccountType.Price: + if (!productOnly) { + this.handlePriceAccount(key, account, slot) + } + break + case AccountType.Test: + case AccountType.Permission: + break + default: + throw new Error(`Unknown account type: ${base.type}. Try upgrading pyth-client.`) + } } + } catch (error: any) { + console.error(`Failed to parse account with key ${key.toString()}: ${error.message}`) } } From 38dc20310f4efc54a88a767d92d134ddbbba1e5a Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Fri, 8 Sep 2023 14:34:02 +0900 Subject: [PATCH 2/2] throw error instead of console.error --- src/PythConnection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PythConnection.ts b/src/PythConnection.ts index 877d84a..214b2ed 100644 --- a/src/PythConnection.ts +++ b/src/PythConnection.ts @@ -102,7 +102,7 @@ export class PythConnection { } } } catch (error: any) { - console.error(`Failed to parse account with key ${key.toString()}: ${error.message}`) + throw new Error(`Failed to parse account with key ${key.toString()}: ${error.message}`) } }