Skip to content

Commit

Permalink
chore: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adnpark committed Apr 25, 2024
1 parent 06a920e commit cf0cee3
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions packages/core/clients/fallbackKernelAccountClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,46 @@ export const createFallbackKernelAccountClient = <
): KernelAccountClient<TEntryPoint, TTransport, TChain, TSmartAccount> => {
const proxyClient = new Proxy(clients[0], {
get(_target, prop, receiver) {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return async (...args: any[]) => {
for (let i = 0; i < clients.length; i++) {
try {
const value = Reflect.get(clients[i], prop, receiver)
if (typeof value === "function") {
return await value.apply(clients[i], args)
} else {
return value
}
} catch (error) {
console.error(
`Action ${String(prop)} failed with client ${
clients[i].transport.url
}, trying next if available.`,
error
)
// If all clients fail, throw the last encountered error
if (i === clients.length - 1) {
throw error
for (const client of clients) {
const value = Reflect.get(client, prop, receiver)
if (value !== undefined) {
// If the property is a function, wrap it to add fallback logic
if (typeof value === "function") {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return async (...args: any[]) => {
for (let i = 0; i < clients.length; i++) {
try {
const method = Reflect.get(
clients[i],
prop,
receiver
)
if (typeof method === "function") {
// Attempt to call the function on the current client
return await method(...args)
}
} catch (error) {
console.error(
`Action ${String(
prop
)} failed with client ${
client.transport.url
}, trying next if available.`,
error
)
if (i === clients.length - 1) {
throw error
}
}
}
}
}
// For non-function properties, return the first defined value found
return value
}
// If no clients have the property, return undefined
return undefined
}
// If no clients have a defined value for the property, return undefined
return undefined
}
})

Expand Down

0 comments on commit cf0cee3

Please sign in to comment.