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

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

Loading…
Cancel
Save