0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-27 15:52:39 +00:00

Merge branch 'master' into addEditorThemes-#362

This commit is contained in:
G.Ambatte
2023-06-29 13:24:56 +12:00
committed by GitHub
11 changed files with 315 additions and 125 deletions

View File

@@ -1,4 +1,4 @@
/*eslint max-lines: ["warn", {"max": 400, "skipBlankLines": true, "skipComments": true}]*/
/*eslint max-lines: ["warn", {"max": 500, "skipBlankLines": true, "skipComments": true}]*/
// Set working directory to project root
process.chdir(`${__dirname}/..`);
@@ -399,7 +399,6 @@ app.get('/account', asyncHandler(async (req, res, next)=>{
return next();
}));
const nodeEnv = config.get('node_env');
const isLocalEnvironment = config.get('local_environments').includes(nodeEnv);
// Local only
@@ -416,8 +415,7 @@ if(isLocalEnvironment){
//Render the page
const templateFn = require('./../client/template.js');
app.use(asyncHandler(async (req, res, next)=>{
const renderPage = async (req, res)=>{
// Create configuration object
const configuration = {
local : isLocalEnvironment,
@@ -427,7 +425,7 @@ app.use(asyncHandler(async (req, res, next)=>{
};
const props = {
version : require('./../package.json').version,
url : req.originalUrl,
url : req.customUrl || req.originalUrl,
brew : req.brew,
brews : req.brews,
googleBrews : req.googleBrews,
@@ -441,15 +439,20 @@ app.use(asyncHandler(async (req, res, next)=>{
const page = await templateFn('homebrew', title, props)
.catch((err)=>{
console.log(err);
return res.sendStatus(500);
});
return page;
};
//Send rendered page
app.use(asyncHandler(async (req, res, next)=>{
const page = await renderPage(req, res);
if(!page) return;
res.send(page);
}));
//v=====----- Error-Handling Middleware -----=====v//
//Format Errors so all fields will be sent
const replaceErrors = (key, value)=>{
//Format Errors as plain objects so all fields will appear in the string sent
const formatErrors = (key, value)=>{
if(value instanceof Error) {
const error = {};
Object.getOwnPropertyNames(value).forEach(function (key) {
@@ -461,13 +464,30 @@ const replaceErrors = (key, value)=>{
};
const getPureError = (error)=>{
return JSON.parse(JSON.stringify(error, replaceErrors));
return JSON.parse(JSON.stringify(error, formatErrors));
};
app.use((err, req, res, next)=>{
const status = err.status || 500;
app.use(async (err, req, res, next)=>{
const status = err.status || err.code || 500;
console.error(err);
res.status(status).send(getPureError(err));
req.ogMeta = { ...defaultMetaTags,
title : 'Error Page',
description : 'Something went wrong!'
};
req.brew = {
...err,
title : 'Error - Something went wrong!',
text : err.errors?.map((error)=>{return error.message;}).join('\n\n') || err.message || 'Unknown error!',
status : status,
HBErrorCode : err.HBErrorCode ?? '00',
pureError : getPureError(err)
};
req.customUrl= '/error';
const page = await renderPage(req, res);
if(!page) return;
res.send(page);
});
app.use((req, res)=>{