From 741a6c79caf72852e43c64a19002739681effc82 Mon Sep 17 00:00:00 2001 From: Mubashir Shariq Date: Sun, 24 Dec 2023 23:55:15 +0530 Subject: [PATCH 1/4] correctly handling the error --- server/controllers/user.controller.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index bc3e179662..ca0e05e0de 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -29,12 +29,19 @@ const random = (done) => { }); }; -export function findUserByUsername(username, cb) { - User.findByUsername(username, (err, user) => { - cb(user); - }); +export async function findUserByUsername(username) { + try { + const user = await User.findByUsername(username); + return user; + } catch (error) { + // Handling the error appropriately, e.g., log the error, throw a custom error, etc. + console.error('Error finding user by username:', error.message); + throw new Error('Failed to find user by username'); + } } + + export function createUser(req, res, next) { const { username, email } = req.body; const { password } = req.body; From b5234eb9518befc418cf69049523a405032b81ee Mon Sep 17 00:00:00 2001 From: Mubashir Shariq Date: Mon, 25 Dec 2023 00:02:01 +0530 Subject: [PATCH 2/4] correctly handling the error ' --- server/controllers/user.controller.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index ca0e05e0de..8c5f1e4330 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -34,7 +34,6 @@ export async function findUserByUsername(username) { const user = await User.findByUsername(username); return user; } catch (error) { - // Handling the error appropriately, e.g., log the error, throw a custom error, etc. console.error('Error finding user by username:', error.message); throw new Error('Failed to find user by username'); } From a3c1f06c9580160ea9b1a5fdd21afb428a5ac7f5 Mon Sep 17 00:00:00 2001 From: Mubashir Shariq Date: Mon, 25 Dec 2023 00:19:35 +0530 Subject: [PATCH 3/4] correctly handling the error ' --- server/controllers/user.controller.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index 8c5f1e4330..dd9d036f97 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -39,8 +39,6 @@ export async function findUserByUsername(username) { } } - - export function createUser(req, res, next) { const { username, email } = req.body; const { password } = req.body; From c8bd198441acfb10b66fcfb50d662f046962d7c1 Mon Sep 17 00:00:00 2001 From: Mubashir Shariq Date: Mon, 25 Dec 2023 13:02:08 +0530 Subject: [PATCH 4/4] deleted finduserbyusername added directly finduserbyname in aws controller --- server/controllers/aws.controller.js | 24 +++++++++++++++++++----- server/controllers/user.controller.js | 10 ---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/server/controllers/aws.controller.js b/server/controllers/aws.controller.js index 91d3d487be..2fa0fac290 100644 --- a/server/controllers/aws.controller.js +++ b/server/controllers/aws.controller.js @@ -3,7 +3,7 @@ import S3Policy from 's3-policy-v4'; import s3 from '@auth0/s3'; import mongoose from 'mongoose'; import { getProjectsForUserId } from './project.controller'; -import { findUserByUsername } from './user.controller'; +import User from '../models/user'; const { ObjectId } = mongoose.Types; @@ -237,10 +237,24 @@ export function listObjectsInS3ForUser(userId) { export function listObjectsInS3ForUserRequestHandler(req, res) { const { username } = req.user; - findUserByUsername(username, (user) => { + User.findByUsername(username, (err, user) => { + if (err) { + console.error('Error fetching user:', err.message); + res.status(500).json({ error: 'Failed to fetch user' }); + return; + } + if (!user) { + res.status(404).json({ error: 'User not found' }); + return; + } const userId = user.id; - listObjectsInS3ForUser(userId).then((objects) => { - res.json(objects); - }); + listObjectsInS3ForUser(userId) + .then((objects) => { + res.json(objects); + }) + .catch((error) => { + console.error('Error listing objects in S3:', error.message); + res.status(500).json({ error: 'Failed to list objects in S3' }); + }); }); } diff --git a/server/controllers/user.controller.js b/server/controllers/user.controller.js index dd9d036f97..6657506c42 100644 --- a/server/controllers/user.controller.js +++ b/server/controllers/user.controller.js @@ -29,16 +29,6 @@ const random = (done) => { }); }; -export async function findUserByUsername(username) { - try { - const user = await User.findByUsername(username); - return user; - } catch (error) { - console.error('Error finding user by username:', error.message); - throw new Error('Failed to find user by username'); - } -} - export function createUser(req, res, next) { const { username, email } = req.body; const { password } = req.body;