Skip to content

Commit 2ca7b32

Browse files
committed
err handling
1 parent 472e333 commit 2ca7b32

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/components/connections/networks-helpers.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,47 @@ export function createNetworkItem(network) {
125125
`;
126126

127127
item.addEventListener('click', async () => {
128+
hideNetworkError(); // Hide any existing error before attempting to switch
128129
await switchNetwork(network.chainId);
129130
document.querySelector('.network-modal').style.display = 'none';
130131
});
131132

132133
return item;
133134
}
134135

136+
export function showNetworkError(message) {
137+
// Remove any existing error message
138+
hideNetworkError();
139+
140+
// Create error message element
141+
const errorDiv = document.createElement('div');
142+
errorDiv.id = 'networkError';
143+
errorDiv.className = 'error-message';
144+
errorDiv.style.marginTop = '10px';
145+
errorDiv.style.width = '100%';
146+
errorDiv.innerHTML = `
147+
<img src="alert-red.svg" alt="" class="error-message-icon" />
148+
<div class="error-message-text">${message}</div>
149+
`;
150+
151+
// Find the network picker button and insert error after it
152+
const networkButton = document.getElementById('openNetworkPicker');
153+
const cardBody = networkButton.closest('.card-body');
154+
cardBody.appendChild(errorDiv);
155+
156+
// Auto-hide after 5 seconds
157+
setTimeout(() => {
158+
hideNetworkError();
159+
}, 5000);
160+
}
161+
162+
export function hideNetworkError() {
163+
const existingError = document.getElementById('networkError');
164+
if (existingError) {
165+
existingError.remove();
166+
}
167+
}
168+
135169
export async function switchNetwork(chainId) {
136170
if (!globalContext.provider) {
137171
console.error('No provider available');
@@ -146,29 +180,12 @@ export async function switchNetwork(chainId) {
146180
} catch (switchError) {
147181
// This error code indicates that the chain has not been added to MetaMask.
148182
if (switchError.code === 4902) {
149-
try {
150-
const network = NETWORKS.find((n) => n.chainId === chainId);
151-
await globalContext.provider.request({
152-
method: 'wallet_addEthereumChain',
153-
params: [
154-
{
155-
chainId,
156-
chainName: network ? network.name : 'Unknown Network',
157-
nativeCurrency: {
158-
name: 'ETH',
159-
symbol: 'ETH',
160-
decimals: 18,
161-
},
162-
rpcUrls: ['https://rpc.example.com'],
163-
blockExplorerUrls: ['https://explorer.example.com'],
164-
},
165-
],
166-
});
167-
} catch (addError) {
168-
console.error('Error adding network:', addError);
169-
}
183+
const network = NETWORKS.find((n) => n.chainId === chainId);
184+
const networkName = network ? network.name : `Chain ID ${chainId}`;
185+
showNetworkError(`${networkName} is not available in your wallet`);
170186
} else {
171187
console.error('Error switching network:', switchError);
188+
showNetworkError('Failed to switch network');
172189
}
173190
}
174191
}

src/components/connections/networks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
populateNetworkLists,
33
updateCurrentNetworkDisplay,
44
updateActiveNetworkInModal,
5+
hideNetworkError,
56
} from './networks-helpers';
67

78
export function networksComponent(parentContainer) {
@@ -58,11 +59,13 @@ export function networksComponent(parentContainer) {
5859

5960
closeButton.addEventListener('click', () => {
6061
modal.style.display = 'none';
62+
hideNetworkError();
6163
});
6264

6365
modal.addEventListener('click', (e) => {
6466
if (e.target === modal) {
6567
modal.style.display = 'none';
68+
hideNetworkError();
6669
}
6770
});
6871

0 commit comments

Comments
 (0)