Merge pull request #1061 from ethereum/catch-folder-error

catch errors for exposed methods
pull/5370/head
David Disu 4 years ago committed by GitHub
commit 4fc0e2214b
  1. 42
      apps/remix-ide/src/app/files/fileManager.js
  2. 6
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -102,10 +102,14 @@ class FileManager extends Plugin {
/** The current opened file */ /** The current opened file */
file () { file () {
try {
const file = this.currentFile() const file = this.currentFile()
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' }) if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' })
return file return file
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -114,6 +118,7 @@ class FileManager extends Plugin {
* @returns {boolean} true if the path exists * @returns {boolean} true if the path exists
*/ */
exists (path) { exists (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
const result = provider.exists(path, (err, result) => { const result = provider.exists(path, (err, result) => {
@ -122,6 +127,9 @@ class FileManager extends Plugin {
}) })
return result return result
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -154,10 +162,14 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async open (path) { async open (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot open file ${path}`) await this._handleExists(path, `Cannot open file ${path}`)
await this._handleIsFile(path, `Cannot open file ${path}`) await this._handleIsFile(path, `Cannot open file ${path}`)
return this.openFile(path) return this.openFile(path)
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -167,6 +179,7 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async writeFile (path, data) { async writeFile (path, data) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
if (await this.exists(path)) { if (await this.exists(path)) {
await this._handleIsFile(path, `Cannot write file ${path}`) await this._handleIsFile(path, `Cannot write file ${path}`)
@ -176,6 +189,9 @@ class FileManager extends Plugin {
this.emit('fileAdded', path) this.emit('fileAdded', path)
return ret return ret
} }
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -184,10 +200,14 @@ class FileManager extends Plugin {
* @returns {string} content of the file * @returns {string} content of the file
*/ */
async readFile (path) { async readFile (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot read file ${path}`) await this._handleExists(path, `Cannot read file ${path}`)
await this._handleIsFile(path, `Cannot read file ${path}`) await this._handleIsFile(path, `Cannot read file ${path}`)
return this.getFileContent(path) return this.getFileContent(path)
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -197,6 +217,7 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async copyFile (src, dest) { async copyFile (src, dest) {
try {
src = this.limitPluginScope(src) src = this.limitPluginScope(src)
dest = this.limitPluginScope(dest) dest = this.limitPluginScope(dest)
await this._handleExists(src, `Cannot copy from ${src}`) await this._handleExists(src, `Cannot copy from ${src}`)
@ -205,6 +226,9 @@ class FileManager extends Plugin {
const content = await this.readFile(src) const content = await this.readFile(src)
await this.writeFile(dest, content) await this.writeFile(dest, content)
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -214,6 +238,7 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async rename (oldPath, newPath) { async rename (oldPath, newPath) {
try {
oldPath = this.limitPluginScope(oldPath) oldPath = this.limitPluginScope(oldPath)
newPath = this.limitPluginScope(newPath) newPath = this.limitPluginScope(newPath)
await this._handleExists(oldPath, `Cannot rename ${oldPath}`) await this._handleExists(oldPath, `Cannot rename ${oldPath}`)
@ -234,6 +259,9 @@ class FileManager extends Plugin {
} }
return provider.rename(oldPath, newPath, true) return provider.rename(oldPath, newPath, true)
} }
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -242,13 +270,17 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async mkdir (path) { async mkdir (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
if (await this.exists(path)) { if (await this.exists(path)) {
throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` }) throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
} }
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
provider.createDir(path) return provider.createDir(path)
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -257,6 +289,7 @@ class FileManager extends Plugin {
* @returns {string[]} list of the file/directory name in this directory * @returns {string[]} list of the file/directory name in this directory
*/ */
async readdir (path) { async readdir (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
await this._handleExists(path) await this._handleExists(path)
await this._handleIsDir(path) await this._handleIsDir(path)
@ -269,6 +302,9 @@ class FileManager extends Plugin {
resolve(filesProvider) resolve(filesProvider)
}) })
}) })
} catch (e) {
throw new Error(e)
}
} }
/** /**
@ -277,11 +313,15 @@ class FileManager extends Plugin {
* @returns {void} * @returns {void}
*/ */
async remove (path) { async remove (path) {
try {
path = this.limitPluginScope(path) path = this.limitPluginScope(path)
await this._handleExists(path, `Cannot remove file or directory ${path}`) await this._handleExists(path, `Cannot remove file or directory ${path}`)
const provider = this.fileProviderOf(path) const provider = this.fileProviderOf(path)
return await provider.remove(path) return await provider.remove(path)
} catch (e) {
throw new Error(e)
}
} }
init () { init () {

@ -335,7 +335,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
}) })
} catch (error) { } catch (error) {
return modal('File Creation Failed', error.message, { return modal('File Creation Failed', typeof error === 'string' ? error : error.message, {
label: 'Close', label: 'Close',
fn: async () => {} fn: async () => {}
}, null) }, null)
@ -360,7 +360,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
return { ...prevState, focusElement: [{ key: newFolderPath, type: 'folder' }] } return { ...prevState, focusElement: [{ key: newFolderPath, type: 'folder' }] }
}) })
} catch (e) { } catch (e) {
return modal('Folder Creation Failed', e.message, { return modal('Folder Creation Failed', typeof e === 'string' ? e : e.message, {
label: 'Close', label: 'Close',
fn: async () => {} fn: async () => {}
}, null) }, null)
@ -404,7 +404,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
await fileManager.rename(oldPath, newPath) await fileManager.rename(oldPath, newPath)
} }
} catch (error) { } catch (error) {
modal('Rename File Failed', 'Unexpected error while renaming: ' + error, { modal('Rename File Failed', 'Unexpected error while renaming: ' + typeof error === 'string' ? error : error.message, {
label: 'Close', label: 'Close',
fn: async () => {} fn: async () => {}
}, null) }, null)

Loading…
Cancel
Save