From 1c68619d811a829d5ef3c68c2be0322cf2c9e21c Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Thu, 16 Jun 2022 11:28:57 +0530 Subject: [PATCH] handle error --- .../workspace/src/lib/actions/index.ts | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 5c220be345..4b19f75948 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -23,6 +23,21 @@ export type UrlParametersType = { url: string } +const basicWorkspaceInit = async (workspaces, workspaceProvider) => { + if (workspaces.length === 0) { + await createWorkspaceTemplate('default_workspace', 'remixDefault') + plugin.setWorkspace({ name: 'default_workspace', isLocalhost: false }) + dispatch(setCurrentWorkspace('default_workspace')) + await loadWorkspacePreset('remixDefault') + } else { + if (workspaces.length > 0) { + workspaceProvider.setWorkspace(workspaces[workspaces.length - 1]) + plugin.setWorkspace({ name: workspaces[workspaces.length - 1], isLocalhost: false }) + dispatch(setCurrentWorkspace(workspaces[workspaces.length - 1])) + } + } +} + export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.Dispatch) => { if (filePanelPlugin) { plugin = filePanelPlugin @@ -50,27 +65,22 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. const contractAddress = route.split('/')[2] const network = {id: 1, name: 'main'} const target = `/${network.name}` - const data = await fetchContractFromEtherscan(plugin, network, contractAddress, target) - await createWorkspaceTemplate('etherscan-code-sample', 'code-template') - plugin.setWorkspace({ name: 'etherscan-code-sample', isLocalhost: false }) - dispatch(setCurrentWorkspace('etherscan-code-sample')) - const filePath = Object.keys(data.compilationTargets)[0] - await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content']) - plugin.on('editor', 'editorMounted', async () => await plugin.fileManager.openFile(filePath)) - } - } else { - if (workspaces.length === 0) { - await createWorkspaceTemplate('default_workspace', 'remixDefault') - plugin.setWorkspace({ name: 'default_workspace', isLocalhost: false }) - dispatch(setCurrentWorkspace('default_workspace')) - await loadWorkspacePreset('remixDefault') - } else { - if (workspaces.length > 0) { - workspaceProvider.setWorkspace(workspaces[workspaces.length - 1]) - plugin.setWorkspace({ name: workspaces[workspaces.length - 1], isLocalhost: false }) - dispatch(setCurrentWorkspace(workspaces[workspaces.length - 1])) + try { + const data = await fetchContractFromEtherscan(plugin, network, contractAddress, target) + await createWorkspaceTemplate('etherscan-code-sample', 'code-template') + plugin.setWorkspace({ name: 'etherscan-code-sample', isLocalhost: false }) + dispatch(setCurrentWorkspace('etherscan-code-sample')) + const filePath = Object.keys(data.compilationTargets)[0] + await workspaceProvider.set(filePath, data.compilationTargets[filePath]['content']) + plugin.on('editor', 'editorMounted', async () => await plugin.fileManager.openFile(filePath)) + } catch (error) { + if(error.message.includes('unable to retrieve contract data') || error.message.includes('unable to try fetching the source code')) + plugin.call('notification', 'toast', error.message) + await basicWorkspaceInit(workspaces, workspaceProvider) } } + } else { + await basicWorkspaceInit(workspaces, workspaceProvider) } listenOnPluginEvents(plugin)