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 -24
View File
@@ -1,4 +1,4 @@
const logger = require("../modules/logger")
const utils = require("../modules/utils")
const database = require("../modules/database")
const { DefaultError } = require("../errors/errors")
@@ -15,9 +15,7 @@ async function insertLegacyClientSessions(sessionId, uuid) {
throw new DefaultError(500, "Internal Server Error", "Unknown DB Error")
}
} 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)
}
}
@@ -39,8 +37,7 @@ async function validateLegacyClientSession(sessionId, uuid) {
}
}
} catch (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)
}
}
@@ -54,8 +51,7 @@ async function getBlockedServers() {
blockedServers: blockedServers.map(bannedServer => ({ sha1: bannedServer.hashedIp }))
}
} catch (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)
}
}
@@ -74,11 +70,7 @@ async function getActiveSkin(uuid) {
}
return { code: 200, data: skin || null }
} 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)
}
}
@@ -97,11 +89,7 @@ async function getActiveCape(uuid) {
}
return { code: 200, data: cape || null }
} 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)
}
}
@@ -115,8 +103,7 @@ async function getProfileActionsList(uuid) {
return { code: 200, data: actions }
} catch (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)
}
}
@@ -135,8 +122,7 @@ async function saveServerSession(uuid, accessToken, serverId, ip) {
return { code: 200, success: result.affectedRows > 0 }
} catch (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)
}
}
@@ -157,8 +143,7 @@ async function getServerSession(uuid, serverId) {
return { code: 200, valid: true, ip: session.ip }
} catch (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)
}
}