Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add try-catch block to handle parsing errors #74

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions src/PythConnection.ts
Original file line number Diff line number Diff line change
@@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: format


const ONES = '11111111111111111111111111111111'

Expand Down Expand Up @@ -78,27 +78,31 @@ export class PythConnection {
}

private handleAccount(key: PublicKey, account: AccountInfo<Buffer>, 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) {
throw new Error(`Failed to parse account with key ${key.toString()}: ${error.message}`)
}
}

Expand Down