Skip to content

Fix timeout errors #3099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions server/controllers/aws.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export async function copyObjectInS3(url, userId) {
Key: objectKey
};

await s3Client.send(new HeadObjectCommand(headParams));
try {
await s3Client.send(new HeadObjectCommand(headParams));
} catch (error) {
console.error('Error fetching object metadata: ', error);
throw error;
}

const params = {
Bucket: process.env.S3_BUCKET,
Expand All @@ -128,9 +133,13 @@ export async function copyObjectInS3(url, userId) {
}

export async function copyObjectInS3RequestHandler(req, res) {
const { url } = req.body;
const newUrl = await copyObjectInS3(url, req.user.id);
res.json({ url: newUrl });
try {
const { url } = req.body;
const newUrl = await copyObjectInS3(url, req.user.id);
res.json({ url: newUrl });
} catch (error) {
res.status(500).json({ error: error.message });
}
}

export async function moveObjectToUserInS3(url, userId) {
Expand Down
45 changes: 19 additions & 26 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ userSchema.statics.findByUsername = function findByUsername(

/**
*
* Queries User collection using email or username with optional callback.
* Queries User collection using email or username.
* This function will determine automatically whether the data passed is
* a username or email, unless you specify options.valueType
*
Expand All @@ -276,37 +276,30 @@ userSchema.statics.findByUsername = function findByUsername(
* default query for username or email, defaults
* to false
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
* @callback [cb] - Optional error-first callback that passes User document
* @return {Promise<Object>} - Returns Promise fulfilled by User document
*/
userSchema.statics.findByEmailOrUsername = function findByEmailOrUsername(
value,
options,
cb
options
) {
let isEmail;
if (options && options.valueType) {
isEmail = options.valueType === 'email';
} else {
isEmail = value.indexOf('@') > -1;
}
const isEmail = options?.valueType
? options.valueType === 'email'
: value.includes('@');

const query = isEmail ? { email: value } : { username: value };
const queryOptions = {
collation: { locale: 'en', strength: 2 },
maxTimeMS: 10000 // Set a timeout of 10 seconds to help prevent long-running queries
};
const queryPromise = this.findOne(query, queryOptions).exec();

// do the case insensitive stuff
if (
(arguments.length === 3 && options.caseInsensitive) ||
(arguments.length === 2 &&
typeof options === 'object' &&
options.caseInsensitive)
) {
const query = isEmail ? { email: value } : { username: value };
return this.findOne(query)
.collation({ locale: 'en', strength: 2 })
.exec(cb);
}
const callback = typeof options === 'function' ? options : cb;
if (isEmail) {
return this.findByEmail(value, callback);
}
return this.findByUsername(value, callback);
// TODO: Handling options should be figured out. At the moment, I think scenarios where it's currently used can be case insensitive?
// if (options?.caseInsensitive) {
// return queryPromise;
// }

return queryPromise;
};

/**
Expand Down
18 changes: 14 additions & 4 deletions server/utils/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ class Mail {
}

async sendMail(mailOptions) {
const response = await this.client.sendMail(mailOptions);
return response;
try {
const response = await this.client.sendMail(mailOptions);
return response;
} catch (error) {
console.error('Failed to send email: ', error);
throw new Error('Email failed to send.');
}
}

async send(data) {
Expand All @@ -31,8 +36,13 @@ class Mail {
html: data.html
};

const response = await this.sendMail(mailOptions);
return response;
try {
const response = await this.sendMail(mailOptions);
return response;
} catch (error) {
console.error('Error in prepping email.', error);
throw new Error('Error in prepping email.');
}
}
}

Expand Down