mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-16 10:22:42 +00:00
Central Error Handling
This commit is contained in:
5
package-lock.json
generated
5
package-lock.json
generated
@@ -3869,6 +3869,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"express-async-handler": {
|
||||||
|
"version": "1.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/express-async-handler/-/express-async-handler-1.1.4.tgz",
|
||||||
|
"integrity": "sha512-HdmbVF4V4w1q/iz++RV7bUxIeepTukWewiJGkoCKQMtvPF11MLTa7It9PRc/reysXXZSEyD4Pthchju+IUbMiQ=="
|
||||||
|
},
|
||||||
"express-static-gzip": {
|
"express-static-gzip": {
|
||||||
"version": "2.1.1",
|
"version": "2.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/express-static-gzip/-/express-static-gzip-2.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/express-static-gzip/-/express-static-gzip-2.1.1.tgz",
|
||||||
|
|||||||
@@ -49,17 +49,18 @@
|
|||||||
"cookie-parser": "^1.4.5",
|
"cookie-parser": "^1.4.5",
|
||||||
"create-react-class": "^15.7.0",
|
"create-react-class": "^15.7.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
"express-async-handler": "^1.1.4",
|
||||||
"express-static-gzip": "2.1.1",
|
"express-static-gzip": "2.1.1",
|
||||||
"fs-extra": "9.1.0",
|
"fs-extra": "9.1.0",
|
||||||
"googleapis": "67.1.1",
|
"googleapis": "67.1.1",
|
||||||
"jwt-simple": "^0.5.6",
|
"jwt-simple": "^0.5.6",
|
||||||
"less": "^3.13.1",
|
"less": "^3.13.1",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
|
"marked": "2.0.1",
|
||||||
|
"markedLegacy": "npm:marked@^0.3.19",
|
||||||
"moment": "^2.29.1",
|
"moment": "^2.29.1",
|
||||||
"mongoose": "^5.12.0",
|
"mongoose": "^5.12.0",
|
||||||
"nanoid": "3.1.21",
|
"nanoid": "3.1.21",
|
||||||
"markedLegacy": "npm:marked@^0.3.19",
|
|
||||||
"marked": "2.0.1",
|
|
||||||
"nconf": "^0.11.2",
|
"nconf": "^0.11.2",
|
||||||
"prop-types": "15.7.2",
|
"prop-types": "15.7.2",
|
||||||
"query-string": "6.14.1",
|
"query-string": "6.14.1",
|
||||||
|
|||||||
89
server.js
89
server.js
@@ -8,15 +8,7 @@ const homebrewApi = require('./server/homebrew.api.js');
|
|||||||
const GoogleActions = require('./server/googleActions.js');
|
const GoogleActions = require('./server/googleActions.js');
|
||||||
const serveCompressedStaticAssets = require('./server/static-assets.mv.js');
|
const serveCompressedStaticAssets = require('./server/static-assets.mv.js');
|
||||||
const sanitizeFilename = require('sanitize-filename');
|
const sanitizeFilename = require('sanitize-filename');
|
||||||
|
const asyncHandler = require('express-async-handler');
|
||||||
// //Custom Error
|
|
||||||
// class ErrorHandler extends Error {
|
|
||||||
// constructor(statusCode, message) {
|
|
||||||
// super();
|
|
||||||
// this.statusCode = statusCode;
|
|
||||||
// this.message = message;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
//Get the brew object from the HB database or Google Drive
|
//Get the brew object from the HB database or Google Drive
|
||||||
const getBrewFromId = async (id, accessType)=>{
|
const getBrewFromId = async (id, accessType)=>{
|
||||||
@@ -33,7 +25,6 @@ const getBrewFromId = async (id, accessType)=>{
|
|||||||
.catch((err)=>{throw err;});
|
.catch((err)=>{throw err;});
|
||||||
brew.sanatize(true);
|
brew.sanatize(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return brew;
|
return brew;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -93,15 +84,9 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
return res.sendFile(`${__dirname}/robots.txt`);
|
return res.sendFile(`${__dirname}/robots.txt`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// app.get('/error', (req, res)=>{
|
|
||||||
// throw new ErrorHandler(404, 'User with the specified email does not exist');
|
|
||||||
// });
|
|
||||||
|
|
||||||
|
|
||||||
//Source page
|
//Source page
|
||||||
app.get('/source/:id', async (req, res)=>{
|
app.get('/source/:id', asyncHandler(async (req, res)=>{
|
||||||
const brew = await getBrewFromId(req.params.id, 'share')
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
.catch((err)=>{next(err);});
|
|
||||||
|
|
||||||
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
||||||
let text = brew.text;
|
let text = brew.text;
|
||||||
@@ -110,13 +95,11 @@ app.get('/source/:id', async (req, res)=>{
|
|||||||
}
|
}
|
||||||
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
||||||
res.status(200).send(text);
|
res.status(200).send(text);
|
||||||
});
|
}));
|
||||||
|
|
||||||
//Download brew source page
|
//Download brew source page
|
||||||
app.get('/download/:id', async (req, res)=>{
|
app.get('/download/:id', asyncHandler(async (req, res)=>{
|
||||||
const brew = await getBrewFromId(req.params.id, 'share')
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
.catch((err)=>{next(err);});
|
|
||||||
|
|
||||||
const prefix = 'HB - ';
|
const prefix = 'HB - ';
|
||||||
|
|
||||||
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
||||||
@@ -127,7 +110,7 @@ app.get('/download/:id', async (req, res)=>{
|
|||||||
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
||||||
});
|
});
|
||||||
res.status(200).send(brew.text);
|
res.status(200).send(brew.text);
|
||||||
});
|
}));
|
||||||
|
|
||||||
//User Page
|
//User Page
|
||||||
app.get('/user/:username', async (req, res, next)=>{
|
app.get('/user/:username', async (req, res, next)=>{
|
||||||
@@ -155,28 +138,23 @@ app.get('/user/:username', async (req, res, next)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
//Edit Page
|
//Edit Page
|
||||||
app.get('/edit/:id', async (req, res, next)=>{
|
app.get('/edit/:id', asyncHandler(async (req, res, next)=>{
|
||||||
res.header('Cache-Control', 'no-cache, no-store'); //reload the latest saved brew when pressing back button, not the cached version before save.
|
res.header('Cache-Control', 'no-cache, no-store'); //reload the latest saved brew when pressing back button, not the cached version before save.
|
||||||
const brew = await getBrewFromId(req.params.id, 'edit')
|
const brew = await getBrewFromId(req.params.id, 'edit');
|
||||||
.catch((err)=>{next(err);});
|
|
||||||
|
|
||||||
req.brew = brew;
|
req.brew = brew;
|
||||||
return next();
|
return next();
|
||||||
});
|
}));
|
||||||
|
|
||||||
//New Page
|
//New Page
|
||||||
app.get('/new/:id', async (req, res, next)=>{
|
app.get('/new/:id', asyncHandler(async (req, res, next)=>{
|
||||||
const brew = await getBrewFromId(req.params.id, 'share')
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
.catch((err)=>{next(err);});
|
|
||||||
|
|
||||||
req.brew = brew;
|
req.brew = brew;
|
||||||
return next();
|
return next();
|
||||||
});
|
}));
|
||||||
|
|
||||||
//Share Page
|
//Share Page
|
||||||
app.get('/share/:id', async (req, res, next)=>{
|
app.get('/share/:id', asyncHandler(async (req, res, next)=>{
|
||||||
const brew = await getBrewFromId(req.params.id, 'share')
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
.catch((err)=>{next(err);});
|
|
||||||
|
|
||||||
if(req.params.id.length > 12) {
|
if(req.params.id.length > 12) {
|
||||||
const googleId = req.params.id.slice(0, -12);
|
const googleId = req.params.id.slice(0, -12);
|
||||||
@@ -189,18 +167,16 @@ app.get('/share/:id', async (req, res, next)=>{
|
|||||||
|
|
||||||
req.brew = brew;
|
req.brew = brew;
|
||||||
return next();
|
return next();
|
||||||
});
|
}));
|
||||||
|
|
||||||
//Print Page
|
//Print Page
|
||||||
app.get('/print/:id', async (req, res, next)=>{
|
app.get('/print/:id', asyncHandler(async (req, res, next)=>{
|
||||||
const brew = await getBrewFromId(req.params.id, 'share');
|
const brew = await getBrewFromId(req.params.id, 'share');
|
||||||
|
|
||||||
req.brew = brew;
|
req.brew = brew;
|
||||||
return next();
|
return next();
|
||||||
});
|
}));
|
||||||
|
|
||||||
//Render the page
|
//Render the page
|
||||||
//const render = require('.build/render');
|
|
||||||
const templateFn = require('./client/template.js');
|
const templateFn = require('./client/template.js');
|
||||||
app.use((req, res)=>{
|
app.use((req, res)=>{
|
||||||
const props = {
|
const props = {
|
||||||
@@ -217,22 +193,35 @@ app.use((req, res)=>{
|
|||||||
templateFn('homebrew', title = req.brew ? req.brew.title : '', props)
|
templateFn('homebrew', title = req.brew ? req.brew.title : '', props)
|
||||||
.then((page)=>{ res.send(page); })
|
.then((page)=>{ res.send(page); })
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
|
console.log('TEMPLATE ERROR');
|
||||||
console.log(err);
|
console.log(err);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//Error Handling Middleware
|
//v=====----- Error-Handling Middleware -----=====v//
|
||||||
|
//Format Errors so all fields will be sent
|
||||||
|
const replaceErrors = (key, value)=>{
|
||||||
|
if(value instanceof Error) {
|
||||||
|
const error = {};
|
||||||
|
Object.getOwnPropertyNames(value).forEach(function (key) {
|
||||||
|
error[key] = value[key];
|
||||||
|
});
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getPureError = (error)=>{
|
||||||
|
return JSON.parse(JSON.stringify(error, replaceErrors));
|
||||||
|
};
|
||||||
|
|
||||||
app.use((err, req, res, next)=>{
|
app.use((err, req, res, next)=>{
|
||||||
const { statusCode, message } = err;
|
const status = err.status || 500;
|
||||||
console.log('CUSTOM ERROR HANDLER');
|
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(statusCode).json({
|
res.status(status).send(getPureError(err));
|
||||||
status : 'error',
|
|
||||||
statusCode,
|
|
||||||
message
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
//^=====--------------------------------------=====^//
|
||||||
|
|
||||||
const PORT = process.env.PORT || config.get('web_port') || 8000;
|
const PORT = process.env.PORT || config.get('web_port') || 8000;
|
||||||
app.listen(PORT);
|
app.listen(PORT);
|
||||||
|
|||||||
Reference in New Issue
Block a user