Handle syntax errors.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<html lang="en">
|
||||
|
||||
{{> (include_partial "layouts/partials/head") }}
|
||||
|
||||
<body>
|
||||
|
||||
<main>
|
||||
<div class="container" style="max-width: 1280px; margin: 1rem auto;">
|
||||
<h1>Syntax Error:</h1>
|
||||
<pre style="padding: 10px 15px; background-color: #ffe6e6; border: 1px solid red; border-radius: 0.25rem; overflow: auto">{{err}}</pre>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
{{> (include_partial "layouts/partials/scripts") }}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -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,6 +152,39 @@ app.get('/publish', async (req, res) => {
|
||||
await fs.unlink('./dist.zip');
|
||||
});
|
||||
|
||||
app.get('/data', async (req, res) => {
|
||||
let jsonDataFileName = req.query.name ? req.query.name : 'default';
|
||||
const data = await getBlockConfigs(jsonDataFileName);
|
||||
|
||||
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data'));
|
||||
const designPreviewFiles = getListOfDesignPreviewFiles(jsonDataFileName, await fs.readdir('./design/preview'));
|
||||
|
||||
return res.json({
|
||||
dataOptions: dataFiles,
|
||||
designPreview: designPreviewFiles,
|
||||
data,
|
||||
});
|
||||
});
|
||||
|
||||
// Errors handler
|
||||
app.use(handleSyntaxErrors);
|
||||
|
||||
// Static Files
|
||||
app.use(express.static('src'));
|
||||
app.use(express.static('design'));
|
||||
app.use(express.static(projectDir + 'layouts'));
|
||||
|
||||
// BrowserSync
|
||||
const bsOptions = await startBrowserSync();
|
||||
port = bsOptions.port;
|
||||
previewFrameUrl = bsOptions.previewFrameUrl;
|
||||
await open(bsOptions.devToolUrl);
|
||||
|
||||
|
||||
//
|
||||
// Functions
|
||||
//
|
||||
|
||||
function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) {
|
||||
return previewFiles
|
||||
.filter(fileName => {
|
||||
@@ -167,34 +203,6 @@ function getListOfDesignPreviewFiles(jsonDataFileName, previewFiles) {
|
||||
});
|
||||
}
|
||||
|
||||
app.get('/data', async (req, res) => {
|
||||
let jsonDataFileName = req.query.name ? req.query.name : 'default';
|
||||
const data = await getBlockConfigs(jsonDataFileName);
|
||||
|
||||
const dataFiles = prepareListOfDataFiles(await fs.readdir('./data'));
|
||||
const designPreviewFiles = getListOfDesignPreviewFiles(jsonDataFileName, await fs.readdir('./design/preview'));
|
||||
|
||||
return res.json({
|
||||
dataOptions: dataFiles,
|
||||
designPreview: designPreviewFiles,
|
||||
data,
|
||||
});
|
||||
});
|
||||
|
||||
app.use(express.static('src'));
|
||||
app.use(express.static('design'));
|
||||
app.use(express.static(projectDir + 'layouts'));
|
||||
|
||||
// BrowserSync
|
||||
const bsOptions = await startBrowserSync();
|
||||
port = bsOptions.port;
|
||||
previewFrameUrl = bsOptions.previewFrameUrl;
|
||||
await open(bsOptions.devToolUrl);
|
||||
|
||||
/**
|
||||
* Functions
|
||||
*/
|
||||
|
||||
function startBrowserSync() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const listener = app.listen(0, async () => {
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user