Skip to content

Commit fdc62ee

Browse files
committed
catch errors for exposed methods
1 parent a344c17 commit fdc62ee

File tree

2 files changed

+114
-74
lines changed

2 files changed

+114
-74
lines changed

apps/remix-ide/src/app/files/fileManager.js

Lines changed: 111 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ class FileManager extends Plugin {
102102

103103
/** The current opened file */
104104
file () {
105-
const file = this.currentFile()
105+
try {
106+
const file = this.currentFile()
106107

107-
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' })
108-
return file
108+
if (!file) throw createError({ code: 'ENOENT', message: 'No file selected' })
109+
return file
110+
} catch (e) {
111+
throw new Error(e)
112+
}
109113
}
110114

111115
/**
@@ -114,14 +118,18 @@ class FileManager extends Plugin {
114118
* @returns {boolean} true if the path exists
115119
*/
116120
exists (path) {
117-
path = this.limitPluginScope(path)
118-
const provider = this.fileProviderOf(path)
119-
const result = provider.exists(path, (err, result) => {
120-
if (err) return false
121-
return result
122-
})
121+
try {
122+
path = this.limitPluginScope(path)
123+
const provider = this.fileProviderOf(path)
124+
const result = provider.exists(path, (err, result) => {
125+
if (err) return false
126+
return result
127+
})
123128

124-
return result
129+
return result
130+
} catch (e) {
131+
throw new Error(e)
132+
}
125133
}
126134

127135
/**
@@ -154,10 +162,14 @@ class FileManager extends Plugin {
154162
* @returns {void}
155163
*/
156164
async open (path) {
157-
path = this.limitPluginScope(path)
158-
await this._handleExists(path, `Cannot open file ${path}`)
159-
await this._handleIsFile(path, `Cannot open file ${path}`)
160-
return this.openFile(path)
165+
try {
166+
path = this.limitPluginScope(path)
167+
await this._handleExists(path, `Cannot open file ${path}`)
168+
await this._handleIsFile(path, `Cannot open file ${path}`)
169+
return this.openFile(path)
170+
} catch (e) {
171+
throw new Error(e)
172+
}
161173
}
162174

163175
/**
@@ -167,14 +179,18 @@ class FileManager extends Plugin {
167179
* @returns {void}
168180
*/
169181
async writeFile (path, data) {
170-
path = this.limitPluginScope(path)
171-
if (await this.exists(path)) {
172-
await this._handleIsFile(path, `Cannot write file ${path}`)
173-
return await this.setFileContent(path, data)
174-
} else {
175-
const ret = await this.setFileContent(path, data)
176-
this.emit('fileAdded', path)
177-
return ret
182+
try {
183+
path = this.limitPluginScope(path)
184+
if (await this.exists(path)) {
185+
await this._handleIsFile(path, `Cannot write file ${path}`)
186+
return await this.setFileContent(path, data)
187+
} else {
188+
const ret = await this.setFileContent(path, data)
189+
this.emit('fileAdded', path)
190+
return ret
191+
}
192+
} catch (e) {
193+
throw new Error(e)
178194
}
179195
}
180196

@@ -184,10 +200,14 @@ class FileManager extends Plugin {
184200
* @returns {string} content of the file
185201
*/
186202
async readFile (path) {
187-
path = this.limitPluginScope(path)
188-
await this._handleExists(path, `Cannot read file ${path}`)
189-
await this._handleIsFile(path, `Cannot read file ${path}`)
190-
return this.getFileContent(path)
203+
try {
204+
path = this.limitPluginScope(path)
205+
await this._handleExists(path, `Cannot read file ${path}`)
206+
await this._handleIsFile(path, `Cannot read file ${path}`)
207+
return this.getFileContent(path)
208+
} catch (e) {
209+
throw new Error(e)
210+
}
191211
}
192212

193213
/**
@@ -197,14 +217,18 @@ class FileManager extends Plugin {
197217
* @returns {void}
198218
*/
199219
async copyFile (src, dest) {
200-
src = this.limitPluginScope(src)
201-
dest = this.limitPluginScope(dest)
202-
await this._handleExists(src, `Cannot copy from ${src}`)
203-
await this._handleIsFile(src, `Cannot copy from ${src}`)
204-
await this._handleIsFile(dest, `Cannot paste content into ${dest}`)
205-
const content = await this.readFile(src)
206-
207-
await this.writeFile(dest, content)
220+
try {
221+
src = this.limitPluginScope(src)
222+
dest = this.limitPluginScope(dest)
223+
await this._handleExists(src, `Cannot copy from ${src}`)
224+
await this._handleIsFile(src, `Cannot copy from ${src}`)
225+
await this._handleIsFile(dest, `Cannot paste content into ${dest}`)
226+
const content = await this.readFile(src)
227+
228+
await this.writeFile(dest, content)
229+
} catch (e) {
230+
throw new Error(e)
231+
}
208232
}
209233

210234
/**
@@ -214,25 +238,29 @@ class FileManager extends Plugin {
214238
* @returns {void}
215239
*/
216240
async rename (oldPath, newPath) {
217-
oldPath = this.limitPluginScope(oldPath)
218-
newPath = this.limitPluginScope(newPath)
219-
await this._handleExists(oldPath, `Cannot rename ${oldPath}`)
220-
const isFile = await this.isFile(oldPath)
221-
const newPathExists = await this.exists(newPath)
222-
const provider = this.fileProviderOf(oldPath)
223-
224-
if (isFile) {
225-
if (newPathExists) {
226-
modalDialogCustom.alert('File already exists.')
227-
return
228-
}
229-
return provider.rename(oldPath, newPath, false)
230-
} else {
231-
if (newPathExists) {
232-
modalDialogCustom.alert('Folder already exists.')
233-
return
241+
try {
242+
oldPath = this.limitPluginScope(oldPath)
243+
newPath = this.limitPluginScope(newPath)
244+
await this._handleExists(oldPath, `Cannot rename ${oldPath}`)
245+
const isFile = await this.isFile(oldPath)
246+
const newPathExists = await this.exists(newPath)
247+
const provider = this.fileProviderOf(oldPath)
248+
249+
if (isFile) {
250+
if (newPathExists) {
251+
modalDialogCustom.alert('File already exists.')
252+
return
253+
}
254+
return provider.rename(oldPath, newPath, false)
255+
} else {
256+
if (newPathExists) {
257+
modalDialogCustom.alert('Folder already exists.')
258+
return
259+
}
260+
return provider.rename(oldPath, newPath, true)
234261
}
235-
return provider.rename(oldPath, newPath, true)
262+
} catch (e) {
263+
throw new Error(e)
236264
}
237265
}
238266

@@ -242,13 +270,17 @@ class FileManager extends Plugin {
242270
* @returns {void}
243271
*/
244272
async mkdir (path) {
245-
path = this.limitPluginScope(path)
246-
if (await this.exists(path)) {
247-
throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
248-
}
249-
const provider = this.fileProviderOf(path)
273+
try {
274+
path = this.limitPluginScope(path)
275+
if (await this.exists(path)) {
276+
throw createError({ code: 'EEXIST', message: `Cannot create directory ${path}` })
277+
}
278+
const provider = this.fileProviderOf(path)
250279

251-
provider.createDir(path)
280+
return provider.createDir(path)
281+
} catch (e) {
282+
throw new Error(e)
283+
}
252284
}
253285

254286
/**
@@ -257,18 +289,22 @@ class FileManager extends Plugin {
257289
* @returns {string[]} list of the file/directory name in this directory
258290
*/
259291
async readdir (path) {
260-
path = this.limitPluginScope(path)
261-
await this._handleExists(path)
262-
await this._handleIsDir(path)
292+
try {
293+
path = this.limitPluginScope(path)
294+
await this._handleExists(path)
295+
await this._handleIsDir(path)
263296

264-
return new Promise((resolve, reject) => {
265-
const provider = this.fileProviderOf(path)
297+
return new Promise((resolve, reject) => {
298+
const provider = this.fileProviderOf(path)
266299

267-
provider.resolveDirectory(path, (error, filesProvider) => {
268-
if (error) reject(error)
269-
resolve(filesProvider)
300+
provider.resolveDirectory(path, (error, filesProvider) => {
301+
if (error) reject(error)
302+
resolve(filesProvider)
303+
})
270304
})
271-
})
305+
} catch (e) {
306+
throw new Error(e)
307+
}
272308
}
273309

274310
/**
@@ -277,11 +313,15 @@ class FileManager extends Plugin {
277313
* @returns {void}
278314
*/
279315
async remove (path) {
280-
path = this.limitPluginScope(path)
281-
await this._handleExists(path, `Cannot remove file or directory ${path}`)
282-
const provider = this.fileProviderOf(path)
316+
try {
317+
path = this.limitPluginScope(path)
318+
await this._handleExists(path, `Cannot remove file or directory ${path}`)
319+
const provider = this.fileProviderOf(path)
283320

284-
return await provider.remove(path)
321+
return await provider.remove(path)
322+
} catch (e) {
323+
throw new Error(e)
324+
}
285325
}
286326

287327
init () {

libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
335335
}
336336
})
337337
} catch (error) {
338-
return modal('File Creation Failed', error.message, {
338+
return modal('File Creation Failed', typeof error === 'string' ? error : error.message, {
339339
label: 'Close',
340340
fn: async () => {}
341341
}, null)
@@ -360,7 +360,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
360360
return { ...prevState, focusElement: [{ key: newFolderPath, type: 'folder' }] }
361361
})
362362
} catch (e) {
363-
return modal('Folder Creation Failed', e.message, {
363+
return modal('Folder Creation Failed', typeof e === 'string' ? e : e.message, {
364364
label: 'Close',
365365
fn: async () => {}
366366
}, null)
@@ -404,7 +404,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
404404
await fileManager.rename(oldPath, newPath)
405405
}
406406
} catch (error) {
407-
modal('Rename File Failed', 'Unexpected error while renaming: ' + error, {
407+
modal('Rename File Failed', 'Unexpected error while renaming: ' + typeof error === 'string' ? error : error.message, {
408408
label: 'Close',
409409
fn: async () => {}
410410
}, null)

0 commit comments

Comments
 (0)