From f8a46b8148979939be6bfcc7351c884706227612 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Fri, 21 Oct 2022 19:16:03 +0300 Subject: [PATCH] Handle syntax errors. --- layouts/error.hbs | 17 ++++++++++++ server.js | 68 +++++++++++++++++++++++++++++++---------------- 2 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 layouts/error.hbs diff --git a/layouts/error.hbs b/layouts/error.hbs new file mode 100644 index 0000000..7c76136 --- /dev/null +++ b/layouts/error.hbs @@ -0,0 +1,17 @@ + + +{{> (include_partial "layouts/partials/head") }} + + + +
+
+

Syntax Error:

+
{{err}}
+
+
+ +{{> (include_partial "layouts/partials/scripts") }} + + + diff --git a/server.js b/server.js index eaec3fb..626de1c 100755 --- a/server.js +++ b/server.js @@ -38,8 +38,9 @@ buildScriptFiles() * Init server */ -let port = 3000; // This variable is used in *.hbs and it will be updated once BrowserSync is ready. +let port = 3000; // This variable is used in `*.hbs` and it will be updated once BrowserSync is ready. let previewFrameUrl = `http://localhost:${port}`; // This variable is used in *.hbs and it will be updated once BrowserSync is ready. +const dataFiles = prepareListOfDataFiles(await fs.readdir('./data')); const app = express(); const hbs = create({ @@ -61,7 +62,9 @@ app.engine('.hbs', hbs.engine); app.set('view engine', '.hbs'); app.set('views', projectDir + 'layouts'); -const dataFiles = prepareListOfDataFiles(await fs.readdir('./data')); +// +// Routes +// app.get('/', async (req, res) => { let jsonFileName = req.query.data ? req.query.data : 'default'; @@ -149,24 +152,6 @@ app.get('/publish', async (req, res) => { await fs.unlink('./dist.zip'); }); -function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) { - return previewFiles - .filter(fileName => { - return fileName.startsWith(jsonDataFileName + '.'); - }) - .map(fileName => { - const fileData = fileName.split('.'); - const fileFormat = fileData.pop(); - const previewSize = fileData.pop(); - - return { - dataSource: jsonDataFileName, - widthDimension: Number.parseInt(previewSize, 10), - url: `/preview/${fileName}`, - }; - }); -} - app.get('/data', async (req, res) => { let jsonDataFileName = req.query.name ? req.query.name : 'default'; const data = await getBlockConfigs(jsonDataFileName); @@ -181,6 +166,10 @@ app.get('/data', async (req, res) => { }); }); +// Errors handler +app.use(handleSyntaxErrors); + +// Static Files app.use(express.static('src')); app.use(express.static('design')); app.use(express.static(projectDir + 'layouts')); @@ -191,9 +180,28 @@ port = bsOptions.port; previewFrameUrl = bsOptions.previewFrameUrl; await open(bsOptions.devToolUrl); -/** - * Functions - */ + +// +// Functions +// + +function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) { + return previewFiles + .filter(fileName => { + return fileName.startsWith(jsonDataFileName + '.'); + }) + .map(fileName => { + const fileData = fileName.split('.'); + const fileFormat = fileData.pop(); + const previewSize = fileData.pop(); + + return { + dataSource: jsonDataFileName, + widthDimension: Number.parseInt(previewSize, 10), + url: `/preview/${fileName}`, + }; + }); +} function startBrowserSync() { return new Promise((resolve, reject) => { @@ -371,3 +379,17 @@ async function zipProject() { // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand await archive.finalize(); } + +function handleSyntaxErrors(err, req, res, next) { + if (err) { + return res.render('error', { + helpers: { + include_partial: (path) => projectDir + path, + }, + err + }); + } + + next(); +} +