mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 18:32:41 +00:00
Basic ErrorPage functionality
This commit is contained in:
@@ -9,7 +9,7 @@ const EditPage = require('./pages/editPage/editPage.jsx');
|
||||
const UserPage = require('./pages/userPage/userPage.jsx');
|
||||
const SharePage = require('./pages/sharePage/sharePage.jsx');
|
||||
const NewPage = require('./pages/newPage/newPage.jsx');
|
||||
//const ErrorPage = require('./pages/errorPage/errorPage.jsx');
|
||||
const ErrorPage = require('./pages/errorPage/errorPage.jsx');
|
||||
const PrintPage = require('./pages/printPage/printPage.jsx');
|
||||
const AccountPage = require('./pages/accountPage/accountPage.jsx');
|
||||
|
||||
@@ -78,6 +78,7 @@ const Homebrew = createClass({
|
||||
<Route path='/faq' element={<WithRoute el={SharePage} brew={this.props.brew} />} />
|
||||
<Route path='/account' element={<WithRoute el={AccountPage} brew={this.props.brew} uiItems={this.props.brew.uiItems} />} />
|
||||
<Route path='/legacy' element={<WithRoute el={HomePage} brew={this.props.brew} />} />
|
||||
<Route path='/error' element={<WithRoute el={ErrorPage} brew={this.props.brew} />} />
|
||||
<Route path='/' element={<WithRoute el={HomePage} brew={this.props.brew} />} />
|
||||
<Route path='/*' element={<WithRoute el={HomePage} brew={this.props.brew} />} />
|
||||
</Routes>
|
||||
|
||||
@@ -16,12 +16,12 @@ const ErrorPage = createClass({
|
||||
getDefaultProps : function() {
|
||||
return {
|
||||
ver : '0.0.0',
|
||||
errorId : ''
|
||||
errorId : '',
|
||||
text : '# Oops \n We could not find a brew with that id. **Sorry!**',
|
||||
error : {}
|
||||
};
|
||||
},
|
||||
|
||||
text : '# Oops \n We could not find a brew with that id. **Sorry!**',
|
||||
|
||||
render : function(){
|
||||
return <div className='errorPage sitePage'>
|
||||
<Navbar ver={this.props.ver}>
|
||||
@@ -39,7 +39,7 @@ const ErrorPage = createClass({
|
||||
</Navbar>
|
||||
|
||||
<div className='content'>
|
||||
<BrewRenderer text={this.text} />
|
||||
<BrewRenderer text={this.props.brew.text} />
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
@@ -397,6 +397,18 @@ app.get('/account', asyncHandler(async (req, res, next)=>{
|
||||
return next();
|
||||
}));
|
||||
|
||||
app.get('/error', (req, res, next)=>{
|
||||
console.log('ERROR PAGE');
|
||||
const errorCookie = JSON.parse(req.cookies['HOMEBREWERY_Error']) || {};
|
||||
if(errorCookie){ res.cookie('HOMEBREWERY_Error', '', { maxAge: 0 }); }
|
||||
|
||||
console.log(errorCookie);
|
||||
|
||||
req.ogMeta = errorCookie.ogMeta;
|
||||
req.brew = errorCookie.error;
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
const nodeEnv = config.get('node_env');
|
||||
const isLocalEnvironment = config.get('local_environments').includes(nodeEnv);
|
||||
@@ -462,9 +474,24 @@ const getPureError = (error)=>{
|
||||
};
|
||||
|
||||
app.use((err, req, res, next)=>{
|
||||
const status = err.status || 500;
|
||||
console.error(err);
|
||||
res.status(status).send(getPureError(err));
|
||||
console.log(err);
|
||||
const status = err.status || err.code || 500;
|
||||
|
||||
const errorData = {
|
||||
error : {
|
||||
title : 'Error - Something went wrong!',
|
||||
text : err.errors?.map((error)=>{return error.message;}).join('\n\n') || err.message || 'Unknown error!',
|
||||
status : status
|
||||
},
|
||||
ogMeta : { ...defaultMetaTags,
|
||||
title : 'Error Page',
|
||||
description : 'Something went wrong!'
|
||||
}
|
||||
};
|
||||
res.cookie('HOMEBREWERY_Error', JSON.stringify(errorData), { maxAge: 300 * 1000 });
|
||||
|
||||
// res.status(status).send(getPureError(err));
|
||||
res.redirect('/error');
|
||||
});
|
||||
|
||||
app.use((req, res)=>{
|
||||
|
||||
@@ -57,7 +57,10 @@ const api = {
|
||||
googleError = err;
|
||||
});
|
||||
// Throw any error caught while attempting to retrieve Google brew.
|
||||
if(googleError) throw googleError;
|
||||
if(googleError) {
|
||||
// console.log(googleError);
|
||||
throw { ...new Error, ...googleError };
|
||||
}
|
||||
// Combine the Homebrewery stub with the google brew, or if the stub doesn't exist just use the google brew
|
||||
stub = stub ? _.assign({ ...api.excludeStubProps(stub), stubbed: true }, api.excludeGoogleProps(googleBrew)) : googleBrew;
|
||||
}
|
||||
@@ -65,14 +68,14 @@ const api = {
|
||||
const isAuthor = stub?.authors?.includes(req.account?.username);
|
||||
const isInvited = stub?.invitedAuthors?.includes(req.account?.username);
|
||||
if(accessType === 'edit' && (authorsExist && !(isAuthor || isInvited))) {
|
||||
throw `The current logged in user does not have editor access to this brew.
|
||||
throw { ...new Error, message : `The current logged in user does not have editor access to this brew.
|
||||
|
||||
If you believe you should have access to this brew, ask the file owner to invite you as an author by opening the brew, viewing the Properties tab, and adding your username to the "invited authors" list. You can then try to access this document again.`;
|
||||
If you believe you should have access to this brew, ask the file owner to invite you as an author by opening the brew, viewing the Properties tab, and adding your username to the "invited authors" list. You can then try to access this document again.` };
|
||||
}
|
||||
|
||||
// If after all of that we still don't have a brew, throw an exception
|
||||
if(!stub && !stubOnly) {
|
||||
throw 'Brew not found in Homebrewery database or Google Drive';
|
||||
throw { ...new Error, message: 'Brew not found in Homebrewery database or Google Drive' };
|
||||
}
|
||||
|
||||
// Clean up brew: fill in missing fields with defaults / fix old invalid values
|
||||
@@ -181,7 +184,7 @@ If you believe you should have access to this brew, ask the file owner to invite
|
||||
saved = await newHomebrew.save()
|
||||
.catch((err)=>{
|
||||
console.error(err, err.toString(), err.stack);
|
||||
throw `Error while creating new brew, ${err.toString()}`;
|
||||
throw { ...new Error, message: `Error while creating new brew, ${err.toString()}` };
|
||||
});
|
||||
if(!saved) return;
|
||||
saved = saved.toObject();
|
||||
@@ -308,7 +311,7 @@ If you believe you should have access to this brew, ask the file owner to invite
|
||||
await HomebrewModel.deleteOne({ _id: brew._id })
|
||||
.catch((err)=>{
|
||||
console.error(err);
|
||||
throw { status: 500, message: 'Error while removing' };
|
||||
throw { ...new Error, status: 500, message: 'Error while removing' };
|
||||
});
|
||||
} else {
|
||||
if(shouldDeleteGoogleBrew) {
|
||||
@@ -320,7 +323,7 @@ If you believe you should have access to this brew, ask the file owner to invite
|
||||
brew.markModified('authors'); //Mongo will not properly update arrays without markModified()
|
||||
await brew.save()
|
||||
.catch((err)=>{
|
||||
throw { status: 500, message: err };
|
||||
throw { ...new Error, status: 500, message: err };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user