diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index f6dccbdb7..a08a39ea0 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -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({ } /> } /> } /> + } /> } /> } /> diff --git a/client/homebrew/pages/errorPage/errorPage.jsx b/client/homebrew/pages/errorPage/errorPage.jsx index 560ab5625..8e6a25ff7 100644 --- a/client/homebrew/pages/errorPage/errorPage.jsx +++ b/client/homebrew/pages/errorPage/errorPage.jsx @@ -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
@@ -39,7 +39,7 @@ const ErrorPage = createClass({
- +
; } diff --git a/server/app.js b/server/app.js index 22c9d9dac..e292831ca 100644 --- a/server/app.js +++ b/server/app.js @@ -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)=>{ diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 39fa021e5..2271466b8 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -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 }; }); } }