Centralize and standardize database error handling

Introduced a new handleDBError utility in modules/utils.js to centralize database error logging and throwing. Refactored all repositories to use this utility, replacing repetitive error handling and logger calls with a single function call for improved maintainability and consistency.
This commit is contained in:
2026-01-18 18:04:10 +01:00
parent 88f8ee57e1
commit 71627c7041
7 changed files with 91 additions and 183 deletions
+9 -28
View File
@@ -1,4 +1,3 @@
const logger = require("../modules/logger")
const bcrypt = require("bcryptjs")
const database = require("../modules/database")
const { DefaultError } = require("../errors/errors")
@@ -19,9 +18,7 @@ async function getUser(identifier, requirePassword = false) {
return { code: 200, user }
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Internal Server Error", "Please contact an administrator.")
return utils.handleDBError(error)
}
}
@@ -37,11 +34,7 @@ async function register(email, username, password) {
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
console.log(error)
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -56,7 +49,7 @@ async function insertClientSession(accessToken, clientToken, uuid) {
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
}
} catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
return utils.handleDBError(error)
}
}
@@ -70,9 +63,7 @@ async function getPlayerProperties(uuid) {
}
return { code: 200, properties: properties.map(property => { return { name: property.name, value: property.value } }) }
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -91,9 +82,7 @@ async function getClientSession(accessToken, clientToken) {
throw new DefaultError(404, "Client session not found")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -112,9 +101,7 @@ async function validateClientSession(accessToken, clientToken) {
throw new DefaultError(404, "Client session not found for this accessToken/clientToken combination.")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -133,9 +120,7 @@ async function validateClientSessionWithoutClientToken(accessToken) {
throw new DefaultError(404, "Client session not found for this accessToken.")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -153,9 +138,7 @@ async function invalidateClientSession(accessToken, clientToken) {
throw new DefaultError(404, "Client session not found for this accessToken/clientToken combination.")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}
@@ -173,9 +156,7 @@ async function revokeAccessTokens(uuid) {
throw new DefaultError(404, "No access token found for this user.")
}
} catch (error) {
if (error instanceof DefaultError) throw error
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"])
throw new DefaultError(500, "Please contact an administrator.", "InternalServerError")
return utils.handleDBError(error)
}
}