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

chore: timeout rpc tracking and track errors #695

Merged
Merged
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
26 changes: 20 additions & 6 deletions deployables/extension/src/background/rpcTracking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sentry } from '@/sentry'
import { sendMessageToTab } from '@/utils'
import { RpcMessageType } from '@zodiac/messages'
import type { ChainId } from 'ser-kit'
Expand Down Expand Up @@ -41,11 +42,13 @@ export const trackRequests = (): TrackRequestsResult => {

chrome.webRequest.onBeforeRequest.addListener(
(details) => {
trackRequest(state, details).then(({ newEndpoint }) => {
if (newEndpoint) {
onNewRpcEndpointDetected.callListeners()
}
})
trackRequest(state, details)
.then(({ newEndpoint }) => {
if (newEndpoint) {
onNewRpcEndpointDetected.callListeners()
}
})
.catch((error) => sentry.captureException(error))
},
{
urls: ['<all_urls>'],
Expand Down Expand Up @@ -137,7 +140,10 @@ const detectNetworkOfRpcUrl = async (
if (!chainIdPromiseByRpcUrl.has(url)) {
chainIdPromiseByRpcUrl.set(
url,
sendMessageToTab(tabId, { type: RpcMessageType.PROBE_CHAIN_ID, url }),
timeout(
sendMessageToTab(tabId, { type: RpcMessageType.PROBE_CHAIN_ID, url }),
`Could not probe chain ID for url "${url}".`,
),
)
}

Expand Down Expand Up @@ -179,3 +185,11 @@ const trackRpcUrl = (
urls.add(url)
}
}

const timeout = <T>(promise: Promise<T>, errorMessage: string) =>
Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => reject(errorMessage), 10_000),
),
])
Loading