mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 20:59:30 +00:00
Catch database errors and prevent them from crashing the app
This commit is contained in:
@@ -98,11 +98,20 @@ class DatabaseConnectionPool {
|
||||
const prepared = sql.includes('?');
|
||||
|
||||
let data;
|
||||
try {
|
||||
if (!prepared) {
|
||||
[ data ] = await this.#connectionPool.execute(sql);
|
||||
[ data ] = await this.#connectionPool.execute(
|
||||
sql
|
||||
);
|
||||
} else {
|
||||
[ data ] =
|
||||
await this.#connectionPool.execute(sql, params);
|
||||
[ data ] = await this.#connectionPool.execute(
|
||||
sql,
|
||||
params
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
data = [];
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
@@ -72,9 +72,7 @@ class PasswordReset {
|
||||
where userId = ?;
|
||||
`;
|
||||
|
||||
await conn.runQuery(sql, [ u.id ]);
|
||||
|
||||
conn.close();
|
||||
let result = await conn.runQuery(sql, [ u.id ]);
|
||||
|
||||
const [ nonce, token ] = await PasswordReset.hashToken(u);
|
||||
|
||||
@@ -93,13 +91,18 @@ class PasswordReset {
|
||||
values (?, ?, ?, FROM_UNIXTIME(?));
|
||||
`;
|
||||
|
||||
await conn.runQuery(sql, [
|
||||
result = await conn.runQuery(sql, [
|
||||
u.id,
|
||||
token,
|
||||
nonce,
|
||||
expires
|
||||
]);
|
||||
|
||||
if (!result)
|
||||
throw new Error('Could not create password reset');
|
||||
|
||||
conn.close();
|
||||
|
||||
return new PasswordReset(u.id, token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,13 +99,18 @@ class TestTemplate {
|
||||
|
||||
const conn = await new DatabaseConnectionPool();
|
||||
|
||||
await conn.runQuery(sql, [
|
||||
const result = await conn.runQuery(sql, [
|
||||
id,
|
||||
this.id,
|
||||
c.id,
|
||||
epochDate
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
if (!result.length)
|
||||
throw new Error('Could not assign class');
|
||||
|
||||
return new Test(id);
|
||||
}
|
||||
|
||||
@@ -130,13 +135,18 @@ class TestTemplate {
|
||||
(?, ?, ?, ?);
|
||||
`;
|
||||
|
||||
await conn.runQuery(sql, [
|
||||
const result = await conn.runQuery(sql, [
|
||||
id,
|
||||
a.id,
|
||||
name,
|
||||
maxMark
|
||||
]);
|
||||
|
||||
conn.close();
|
||||
|
||||
if (!result)
|
||||
throw new Error('Could not create test template');
|
||||
|
||||
return new TestTemplate(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +94,8 @@ class User {
|
||||
|
||||
return new (require(`./${className}`))(this.id);
|
||||
}
|
||||
|
||||
throw new Error('No user found');
|
||||
})();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user