Skip to content

Commit

Permalink
better error
Browse files Browse the repository at this point in the history
  • Loading branch information
lexoyo committed Jun 23, 2023
1 parent a2562a8 commit 4c949f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
37 changes: 29 additions & 8 deletions src/plugins/DefaultBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,38 @@ exports.del = async function (id) {

// List projects
exports.list = async function () {
const ids = await readdir(FS_ROOT)
return Promise.all(ids.map(async id => ({
id,
...await exports.readData(id),
stats: await stat(path(id) + DATA_FILE_NAME),
})))
try {
const ids = await readdir(FS_ROOT)
return Promise.all(ids.map(async id => ({
id,
...await exports.readData(id),
stats: await stat(path(id) + DATA_FILE_NAME),
})))
} catch (error) {
if (error.code === 'ENOENT') {
console.log(`\nError: Directory ${FS_ROOT} does not exist.\nIf your want to read from a different folder ues the FS_ROOT environment variable or --fs-root command line argument?\n`);
const betterError = new Error(`The directory ${FS_ROOT} does not exist`)
betterError.code = 'ENOENT'
throw betterError
}
throw error
}
}
// Read project data
exports.readData = async function (id) {
const data = await readFile(path(id) + DATA_FILE_NAME)
return JSON.parse(data.toString())
const filePath = path(id) + DATA_FILE_NAME
try {
const data = await readFile(filePath)
return JSON.parse(data.toString())
} catch (error) {
if (error.code === 'ENOENT') {
console.log(`\nError: File ${filePath} or directory ${FS_ROOT} do not exist.\nIf your want to read from a different folder use the FS_ROOT environment variable or --fs-root command line argument?\n`);
const betterError = new Error(`The website ${id} does not exist`)
betterError.code = 'ENOENT'
throw betterError
}
throw error
}
}
// Write project data
exports.writeData = async function (id, data) {
Expand Down
16 changes: 10 additions & 6 deletions src/ts/client/grapesjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,19 @@ export function initEditor(config, grapesJsPlugins) {
editor.StyleManager.addProperty('extra',{ extend: 'filter' })
})

editor.StorageManager.onError = (type: string, err: Error) => {
editor.Modal.open(type === 'load' ? {
// Handle errors
editor.on('storage:error:load', (err) => {
editor.Modal.open({
title: 'Error loading website',
content: `This website could not be loaded.<br><hr>Error: ${err.message}`,
} : {
content: `This website could not be loaded.<br><hr>${err}`,
})
})
editor.on('storage:error:store', (err) => {
editor.Modal.open({
title: 'Error saving website',
content: `This website could not be saved.<br><hr>Error: ${err.message}`,
content: `This website could not be saved.<br><hr>${err}`,
})
}
})
}

export function getEditor() {
Expand Down

0 comments on commit 4c949f6

Please sign in to comment.