0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 14:12:40 +00:00

Differentiate Not an Author from Not logged in

This commit is contained in:
G.Ambatte
2023-06-25 18:10:31 +12:00
parent 4c6de90d82
commit e28b6e7a19
2 changed files with 19 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
const dedent = require('dedent-tabs').default;
const loginUrl = 'https://www.naturalcrit.com/login';
const errorIndex = (props)=>{
return {
'00' : dedent`
@@ -59,24 +61,29 @@ const errorIndex = (props)=>{
`,
'04' : dedent`
## Not logged in
User is not logged in. Please log in [here](${loginUrl}).`,
'05' : dedent`
## No Homebrewery document could be found.
The server could not locate the Homebrewery document.`,
'05' : dedent`
'06' : dedent`
## Unable to save Homebrewery document.
An error occurred wil attempting to save the Homebrewery document.`,
'06' : dedent`
'07' : dedent`
## Unable to delete Homebrewery document.
An error occurred while attempting to remove the Homebrewery document.`,
'07' : dedent`
'08' : dedent`
## Unable to remove user from Homebrewery document.
An error occurred while attempting to remove the current user from the Homebrewery document author list!`
An error occurred while attempting to remove the current user from the Homebrewery document author list!`,
};
};

View File

@@ -68,12 +68,15 @@ 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 { name: 'Access Error', message: 'User is not an Author', status: 401, HBErrorCode: '03', authors: stub.authors };
if(req.account){
throw { name: 'Access Error', message: 'User is not an Author', status: 401, HBErrorCode: '03', authors: stub.authors };
}
throw { name: 'Access Error', message: 'User is not logged in', status: 401, HBErrorCode: '04' };
}
// If after all of that we still don't have a brew, throw an exception
if(!stub && !stubOnly) {
throw { name: 'BrewLoad Error', message: 'Brew not found', status: 404, HBErrorCode: '04' };
throw { name: 'BrewLoad Error', message: 'Brew not found', status: 404, HBErrorCode: '05' };
}
// Clean up brew: fill in missing fields with defaults / fix old invalid values
@@ -182,7 +185,7 @@ const api = {
saved = await newHomebrew.save()
.catch((err)=>{
console.error(err, err.toString(), err.stack);
throw { name: 'BrewSave Error', message: `Error while creating new brew, ${err.toString()}`, status: 500, HBErrorCode: '05' };
throw { name: 'BrewSave Error', message: `Error while creating new brew, ${err.toString()}`, status: 500, HBErrorCode: '06' };
});
if(!saved) return;
saved = saved.toObject();
@@ -309,7 +312,7 @@ const api = {
await HomebrewModel.deleteOne({ _id: brew._id })
.catch((err)=>{
console.error(err);
throw { name: 'BrewDelete Error', message: 'Error while removing', status: 500, HBErrorCode: '06' };
throw { name: 'BrewDelete Error', message: 'Error while removing', status: 500, HBErrorCode: '07' };
});
} else {
if(shouldDeleteGoogleBrew) {
@@ -321,7 +324,7 @@ const api = {
brew.markModified('authors'); //Mongo will not properly update arrays without markModified()
await brew.save()
.catch((err)=>{
throw { name: 'BrewAuthorDelete Error', message: err, status: 500, HBErrorCode: '07' };
throw { name: 'BrewAuthorDelete Error', message: err, status: 500, HBErrorCode: '08' };
});
}
}