Skip to content

Commit

Permalink
console log errors on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkaterina committed Feb 26, 2024
1 parent b418bce commit 600edd8
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 37 deletions.
14 changes: 8 additions & 6 deletions functions/api-controllers/foodDatabaseAPI-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ const parseQuery = async (req, res, next) => {
const response = await fetch(url, options);
const result = await response.json();
res.status(200).json(result);
} catch (error) {
console.error(error);
throw new HttpError('Could not find any results', 404);
} catch (err) {
console.error(err);
const error = HttpError('Could not find any results', 404);
return next(error);
}
}

Expand Down Expand Up @@ -74,9 +75,10 @@ const findNutrients = async (req, res, next) => {
const response = await fetch(url, options);
const result = await response.json();
res.status(200).json(result);
} catch (error) {
console.error(error);
throw new HttpError('Could not fetch nutrients', 404);
} catch (err) {
console.error(err);
const error = new HttpError('Could not fetch nutrients', 404);
return next(error);
}
}

Expand Down
10 changes: 10 additions & 0 deletions functions/db-controllers/food-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ let userWithFood;
try {
userWithFood = await User.findById(userId).populate('foods');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not find food. Try again later.', 500
);
return next(error);
}

if (!userWithFood) {
console.error('Could not find user by id');
return next(
new HttpError('Could not find food. Try again later.', 404)
);
Expand All @@ -43,11 +45,13 @@ let user;
try {
user = await User.findById(req.userData.userId);
} catch (err) {
console.error(err);
const error = new HttpError('Could not add food to favorites. Try again later.', 500);
return next(error);
}

if (!user) {
console.error('Could not find user by id');
const error = new HttpError('Could not add food to favorites. Try again later.', 404);
return next(error);
}
Expand All @@ -56,6 +60,7 @@ let existingFood
try {
existingFood = await Food.findOne({ "food.food.label": food.food.label, "creator": req.userData.userId})
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
Expand All @@ -79,6 +84,7 @@ try {
await user.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add food to favorites. Try again later.',
500
Expand All @@ -96,6 +102,7 @@ let food;
try {
food = await Food.findById(foodId).populate('creator');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
Expand All @@ -104,11 +111,13 @@ try {
}

if (!food) {
console.error('Could not find food by id');
const error = new HttpError('Could not delete food. Try again later.', 404);
return next(error);
}

if (food.creator.id !== req.userData.userId) {
console.error('Not authorized')
const error = new HttpError(
'You are not allowed to delete this food.',
401
Expand All @@ -124,6 +133,7 @@ try {
await food.creator.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete food. Try again later.',
500
Expand Down
22 changes: 14 additions & 8 deletions functions/db-controllers/menu-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let userWithMenu;
try {
userWithMenu = await User.findById(userId).populate('menus');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not find menu. Try again later.',
500
Expand All @@ -19,6 +20,7 @@ try {
}

if (!userWithMenu) {
console.error('Could not find menu by id.')
return next(
new HttpError('Could not find menu. Try again later.', 404)
);
Expand Down Expand Up @@ -46,22 +48,22 @@ let user;
try {
user = await User.findById(req.userData.userId);
} catch (err) {
console.log('could not find user');
console.error(err);
const error = new HttpError('Could not add menu to favorites. Try again later.', 500);
return next(error);
}

if (!user) {
console.error('Could not find user by id.');
const error = new HttpError('Could not add menu to favorites. Try again later.', 404);
console.log(error);
return next(error);
}

let existingMenu
try {
existingMenu = await Menu.findOne({ "menu.name": menu.name, "creator": req.userData.userId})
} catch (err) {
console.log('could not find menu');
console.error(err);
const error = new HttpError(
'Could not add menu to favorites. Try again later.',
500
Expand All @@ -74,7 +76,6 @@ if (existingMenu) {
'Menu with this name exists already.',
422
);
console.log(error);
return next(error);
}

Expand All @@ -86,7 +87,7 @@ try {
await user.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.log(err)
console.error(err);
const error = new HttpError(
'Could not add menu to favorites. Try again later.',
500
Expand All @@ -106,22 +107,23 @@ console.log(updatedMenu);
let menu;
try {
menu = await Menu.findById(menuId);
// console.log(menu);
} catch (err) {
console.error(err);
const error = new HttpError(
console.log('could not find menu'),
'Could not update menu in favorites. Try again later.',
500
);
return next(error);
}

if (!menu) {
console.error('Could not find menu by id.');
const error = new HttpError('Could not update menu in favorites. Try again later.', 404);
return next(error);
}

if (menu.creator.toString() !== req.userData.userId) {
console.error('Not authorized.');
const error = new HttpError('You are not allowed to edit this menu.', 401);
return next(error);
}
Expand All @@ -132,7 +134,7 @@ menu.menu = updatedMenu;
try {
await menu.save();
} catch (err) {
console.log('could not save menu')
console.error(err);
const error = new HttpError(
'Could not update menu in favorites. Try again later.',
500
Expand All @@ -150,6 +152,7 @@ let menu;
try {
menu = await Menu.findById(menuId).populate('creator');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete menu. Try again later.',
500
Expand All @@ -158,11 +161,13 @@ try {
}

if (!menu) {
console.error('Could not find menu by id.');
const error = new HttpError('Could not delete menu. Try again later.', 404);
return next(error);
}

if (menu.creator.id !== req.userData.userId) {
console.error('Not authorized');
const error = new HttpError(
'You are not allowed to delete this menu.',
401
Expand All @@ -178,6 +183,7 @@ try {
await menu.creator.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete menu. Try again later.',
500
Expand Down
14 changes: 14 additions & 0 deletions functions/db-controllers/recipe-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let userWithRecipe;
try {
userWithRecipe = await User.findById(userId).populate('recipes');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not find recipe. Try again later.',
500
Expand All @@ -21,6 +22,7 @@ try {
}

if (!userWithRecipe) {
console.error('Could not find user by id.');
return next(
new HttpError('Could not find recipe. Try again later.', 404)
);
Expand Down Expand Up @@ -48,11 +50,13 @@ let user;
try {
user = await User.findById(req.userData.userId);
} catch (err) {
console.error(err);
const error = new HttpError('Could not add recipe to favorites. Try again later.', 500);
return next(error);
}

if (!user) {
console.error('Could not find user by id.');
const error = new HttpError('Could not add recipe to favorites. Try again later.', 404);
return next(error);
}
Expand All @@ -61,6 +65,7 @@ let existingRecipe
try {
existingRecipe = await Recipe.findOne({ "recipe.name": parsedRecipe.name, "creator": req.userData.userId});
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not add recipe to favorites. Try again later.',
500
Expand All @@ -84,6 +89,7 @@ try {
await user.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Creating recipe failed, please try again.Could not add recipe to favorites. Try again later.',
);
Expand All @@ -104,6 +110,7 @@ let recipe;
try {
recipe = await Recipe.findById(recipeId,);
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not update recipe in favorites. Try again later.',
500
Expand All @@ -112,11 +119,13 @@ try {
}

if (!recipe) {
console.error('Could not find recipe by id.');
const error = new HttpError('Could not update recipe in favorites. Try again later.', 404);
return next(error);
}

if (recipe.creator.toString() !== req.userData.userId) {
console.error('Not authorized.');
const error = new HttpError('You are not allowed to edit this recipe.', 401);
return next(error);
}
Expand All @@ -133,6 +142,7 @@ if(undatedImage) {
try {
await recipe.save();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not update recipe in favorites. Try again later.',
500
Expand All @@ -150,6 +160,7 @@ let recipe;
try {
recipe = await Recipe.findById(recipeId).populate('creator');
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete recipe. Try again later.',
500
Expand All @@ -158,11 +169,13 @@ try {
}

if (!recipe) {
console.error('Could not find recipe by id.');
const error = new HttpError('Could not delete recipe. Try again later.', 404);
return next(error);
}

if (recipe.creator.id !== req.userData.userId) {
console.error('Not authorized.');
const error = new HttpError(
'You are not allowed to delete this recipe.',
401
Expand All @@ -180,6 +193,7 @@ try {
await recipe.creator.save({ session: sess });
await sess.commitTransaction();
} catch (err) {
console.error(err);
const error = new HttpError(
'Could not delete recipe. Try again later.',
500
Expand Down
Loading

0 comments on commit 600edd8

Please sign in to comment.