Skip to content

Commit 1053155

Browse files
authored
Merge pull request #3099 from processing/fix/timeout-errors
Fix timeout errors
2 parents 9190d0e + d460fbc commit 1053155

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

server/controllers/aws.controller.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ export async function copyObjectInS3(url, userId) {
109109
Key: objectKey
110110
};
111111

112-
await s3Client.send(new HeadObjectCommand(headParams));
112+
try {
113+
await s3Client.send(new HeadObjectCommand(headParams));
114+
} catch (error) {
115+
console.error('Error fetching object metadata: ', error);
116+
throw error;
117+
}
113118

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

130135
export async function copyObjectInS3RequestHandler(req, res) {
131-
const { url } = req.body;
132-
const newUrl = await copyObjectInS3(url, req.user.id);
133-
res.json({ url: newUrl });
136+
try {
137+
const { url } = req.body;
138+
const newUrl = await copyObjectInS3(url, req.user.id);
139+
res.json({ url: newUrl });
140+
} catch (error) {
141+
res.status(500).json({ error: error.message });
142+
}
134143
}
135144

136145
export async function moveObjectToUserInS3(url, userId) {

server/models/user.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ userSchema.statics.findByUsername = function findByUsername(
266266

267267
/**
268268
*
269-
* Queries User collection using email or username with optional callback.
269+
* Queries User collection using email or username.
270270
* This function will determine automatically whether the data passed is
271271
* a username or email, unless you specify options.valueType
272272
*
@@ -276,37 +276,30 @@ userSchema.statics.findByUsername = function findByUsername(
276276
* default query for username or email, defaults
277277
* to false
278278
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
279-
* @callback [cb] - Optional error-first callback that passes User document
280279
* @return {Promise<Object>} - Returns Promise fulfilled by User document
281280
*/
282281
userSchema.statics.findByEmailOrUsername = function findByEmailOrUsername(
283282
value,
284-
options,
285-
cb
283+
options
286284
) {
287-
let isEmail;
288-
if (options && options.valueType) {
289-
isEmail = options.valueType === 'email';
290-
} else {
291-
isEmail = value.indexOf('@') > -1;
292-
}
285+
const isEmail = options?.valueType
286+
? options.valueType === 'email'
287+
: value.includes('@');
288+
289+
const query = isEmail ? { email: value } : { username: value };
290+
const queryOptions = {
291+
collation: { locale: 'en', strength: 2 },
292+
maxTimeMS: 10000 // Set a timeout of 10 seconds to help prevent long-running queries
293+
};
294+
const queryPromise = this.findOne(query, queryOptions).exec();
295+
293296
// do the case insensitive stuff
294-
if (
295-
(arguments.length === 3 && options.caseInsensitive) ||
296-
(arguments.length === 2 &&
297-
typeof options === 'object' &&
298-
options.caseInsensitive)
299-
) {
300-
const query = isEmail ? { email: value } : { username: value };
301-
return this.findOne(query)
302-
.collation({ locale: 'en', strength: 2 })
303-
.exec(cb);
304-
}
305-
const callback = typeof options === 'function' ? options : cb;
306-
if (isEmail) {
307-
return this.findByEmail(value, callback);
308-
}
309-
return this.findByUsername(value, callback);
297+
// TODO: Handling options should be figured out. At the moment, I think scenarios where it's currently used can be case insensitive?
298+
// if (options?.caseInsensitive) {
299+
// return queryPromise;
300+
// }
301+
302+
return queryPromise;
310303
};
311304

312305
/**

server/utils/mail.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ class Mail {
1919
}
2020

2121
async sendMail(mailOptions) {
22-
const response = await this.client.sendMail(mailOptions);
23-
return response;
22+
try {
23+
const response = await this.client.sendMail(mailOptions);
24+
return response;
25+
} catch (error) {
26+
console.error('Failed to send email: ', error);
27+
throw new Error('Email failed to send.');
28+
}
2429
}
2530

2631
async send(data) {
@@ -31,8 +36,13 @@ class Mail {
3136
html: data.html
3237
};
3338

34-
const response = await this.sendMail(mailOptions);
35-
return response;
39+
try {
40+
const response = await this.sendMail(mailOptions);
41+
return response;
42+
} catch (error) {
43+
console.error('Error in prepping email.', error);
44+
throw new Error('Error in prepping email.');
45+
}
3646
}
3747
}
3848

0 commit comments

Comments
 (0)